英文:
Which is better, creating functions for onClick or creating Listeners?
问题
I have watched different tutorials on android development from different channels.
One of them makes functions for onClick events and then adds them on the attribute of the View.
Example:
public void change(View v){
((TextView) findViewbyId(R.id.example)).setText("Changed");
}
And then adds this to the attribute of the button.
But the other one implements the View.OnClickListener interface, and it becomes like this:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView output;
private EditText edit;
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.change: {
Toast.makeText(this, "Change button was clicked", Toast.LENGTH_SHORT).show();
} break;
case R.id.input:{
Toast.makeText(this, "Typing..", Toast.LENGTH_SHORT).show();
} break;
default: break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.change);
btn.setOnClickListener(this);
((Button) findViewById(R.id.nothing)).setOnClickListener(this);
edit = findViewById(R.id.input);
edit.setOnClickListener(this);
output = findViewById(R.id.output);
}
}
I'm new to android development, so I was wondering which is better? Which is more used professionally? and why? Thank you!
<details>
<summary>英文:</summary>
I have watched different tutorials on android development from different channels.
One of them makes functions for onClick events and then adds them on the attribute of the View.
Example:
public void change(View v){
((TextView) findViewbyId(R.id.example)).setText("Changed");
}
And then adds this to the attribute of the button.
But the other one implements the View.OnClickListener interface, and it becomes like this:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView output;
private EditText edit;
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.change: {
Toast.makeText(this, "Change button was clicked", Toast.LENGTH_SHORT).show();
} break;
case R.id.input:{
Toast.makeText(this, "Typing..", Toast.LENGTH_SHORT).show();
} break;
default: break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.change);
btn.setOnClickListener(this);
((Button) findViewById(R.id.nothing)).setOnClickListener(this);
edit = findViewById(R.id.input);
edit.setOnClickListener(this);
output = findViewById(R.id.output);
}
Im new to android development, so I was wondering which is better? Which is more used professionally? and why? Thank you!
</details>
# 答案1
**得分**: 1
android:onClick="change"
两种方式都是正确的,你可以选择更合适的方式。如果你只有一个可点击的 `View`,那么定义一个指向某个方法的 XML 属性会生成更少的代码,但如果你有多个可点击的 `View`,那么通过Java代码来管理它们可能更容易(例如在运行时移除/禁用点击处理),并在一个 `onClick` 方法中处理所有点击事件。
请注意,如果你的 `Activity` 中实现了 `View.OnClickListener`,那么你还可以写成:
android:onClick="onClick"
然后你就不必在代码中调用 `setOnClickListener(this)` 了。通常这样做甚至不需要 `findViewById` 和引用到 `View`。
<details>
<summary>英文:</summary>
assuming you are using XML attribute
android:onClick="change"
both ways are proper and you may pick more appropriate. If you have one and only clickable `View` then definining XML attr pointing on some method will produce less code, but if you have more of them then it may be easier for manage all of them (e.g. removing/disabling click handling during runtime) by Java code and handle all clicks in one `onClick` method
note you if you have `implements View.OnClickListener` in your `Activity` then you may also write
android:onClick="onClick"
and then you don't have to call `setOnClickListener(this)` in code. oftenly this makes that you don't even have to `findViewById` and reference to `View`
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论