多个按钮的一个 OnClickHandler [英] One OnClickHandler for multiple Buttons
本文介绍了多个按钮的一个 OnClickHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现自己一直在做这样的事情:
I find myself doing things like this all the time:
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(menuButtonListener);
button2.setOnClickListener(menuButtonListener);
button3.setOnClickListener(menuButtonListener);
...
和
private OnClickListener myButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1 :
...
有没有更好的方法来设置 OnClickListener?
Is there a better way to set the OnClickListener?
推荐答案
您也可以使用 android:onclick 属性在布局 xml 中设置它.
You can also set it in your layout xml using the android:onclick attribute.
android:onClick="onClick"
然后在您的活动类中添加 onClick 方法.
Then in your activity class add the onClick method.
public void onClick(View v) {
...
这是文档.
这篇关于多个按钮的一个 OnClickHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文