Android OnClickListener - 识别按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3320115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android OnClickListener - identify a button
提问by xpepermint
I have the activity:
我有活动:
public class Mtest extends Activity {
Button b1;
Button b2;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler);
b2.setOnClickListener(myhandler);
...
}
View.OnClickListener myhandler = new View.OnClickListener() {
public void onClick(View v) {
// MY QUESTION STARTS HERE!!!
// IF b1 do this
// IF b2 do this
// MY QUESTION ENDS HERE!!!
}
}
}
How do I check which button has been clicked?
如何检查哪个按钮被点击?
回答by Cristian
You will learn the way to do it, in an easy way, is:
您将学习的方法很简单,就是:
public class Mtest extends Activity {
Button b1;
Button b2;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
...
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
}
};
}
Or, if you are working with just one clicklistener, you can do:
或者,如果您只使用一个点击监听器,您可以执行以下操作:
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.b1:
// it was the first button
break;
case R.id.b2:
// it was the second button
break;
}
}
}
Though, I don't recommend doing it that way since you will have to add an if
for each button you use. That's hard to maintain.
不过,我不建议这样做,因为您必须为使用if
的每个按钮添加一个。这很难维持。
回答by Chronos
Or you can try the same but without listeners. On your button XML definition:
或者您可以尝试相同但没有听众的方法。在您的按钮 XML 定义上:
android:onClick="ButtonOnClick"
And in your code define the method ButtonOnClick
:
并在您的代码中定义方法ButtonOnClick
:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case R.id.button1:
doSomething1();
break;
case R.id.button2:
doSomething2();
break;
}
}
回答by Saad Farooq
I prefer:
我更喜欢:
class MTest extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
...
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
...
}
And then:
进而:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
....
break;
case R.id.b2:
....
break;
}
}
Switch
-case
is easier to maintain than if
-else
, and this implementation doesn't require making many class variables.
Switch
-case
比if
-更容易维护else
,并且这个实现不需要创建很多类变量。
回答by Suragch
Five Ways to Wire Up an Event Listeneris a great article overviewing the various ways to set up a single event listener. Let me expand that here for multiple listeners.
连接事件侦听器的五种方法是一篇很棒的文章,概述了设置单个事件侦听器的各种方法。让我在这里为多个听众扩展它。
1. Member Class
1.会员类
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//attach an instance of HandleClick to the Button
HandleClick handleClick = new HandleClick();
findViewById(R.id.button1).setOnClickListener(handleClick);
findViewById(R.id.button2).setOnClickListener(handleClick);
}
private class HandleClick implements OnClickListener{
public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
// do stuff
break;
case R.id.button2:
// do stuff
break;
}
}
}
}
2. Interface Type
2. 接口类型
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(handleClick);
findViewById(R.id.button2).setOnClickListener(handleClick);
}
private OnClickListener handleClick = new OnClickListener() {
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// do stuff
break;
case R.id.button2:
// do stuff
break;
}
}
};
}
3. Anonymous Inner Class
3. 匿名内部类
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do stuff
}
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do stuff
}
});
}
}
4. Implementation in Activity
4. 在活动中的实施
public class main extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// do stuff
break;
case R.id.button2:
// do stuff
break;
}
}
}
5. Attribute in View Layout for OnClick Events
5. OnClick 事件视图布局中的属性
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void HandleClick(View view) {
switch (view.getId()) {
case R.id.button1:
// do stuff
break;
case R.id.button2:
// do stuff
break;
}
}
}
And in xml:
在 xml 中:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="HandleClick" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="HandleClick" />
回答by lory105
If you don't want to save instances of the 2 button in the class code, follow this BETTER way (this is more clear and fast!!) :
如果您不想在类代码中保存 2 按钮的实例,请按照这种更好的方式(这样更清晰、更快捷!!):
public void buttonPress(View v) {
switch (v.getId()) {
case R.id.button_one:
// do something
break;
case R.id.button_two:
// do something else
break;
case R.id.button_three:
// i'm lazy, do nothing
break;
}
}
回答by ruhalde
Another way of doing it is a single listener from activity , like this:
另一种方法是来自 activity 的单个侦听器,如下所示:
public class MyActivity extends Activity implements OnClickListener {
....... code
//my listener
@Override
public void onClick(View v) {
if (v.getId() == R.id.mybutton) {
DoSomething();
return;
}
if (v.getId() == R.id.mybutton2) {
DoSomething2();
return;
}
}
}
I Like to do it with single IF instead of switch-else, but if you prefer that, then you should do:
我喜欢用单个 IF 而不是 switch-else 来做,但如果你喜欢这样,那么你应该这样做:
//my listener
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.mybutton:
{
DoSomething();
break;
}
case R.id.mybutton2:
{
DoSomething();
break;
}
}
}
回答by ognian
The best way is by switch
-ing between v.getId(). Having separate anonymous OnClickListener for each Button is taking up more memory. Casting View to Button is unnecessary. Using if-else when switch is possible is slower and harder to read. In Android's source you can often notice comparing the references by if-else:
最好的方法是switch
在 v.getId() 之间使用-ing。为每个 Button 设置单独的匿名 OnClickListener 会占用更多内存。不需要将视图转换为按钮。在可以切换时使用 if-else 会更慢且更难阅读。在 Android 的源代码中,您经常会注意到通过 if-else 比较引用:
if (b1 == v) {
// ...
} else if (b2 == v) {
I don't know exactly why they chose this way, but it works too.
我不知道他们为什么选择这种方式,但它也有效。
回答by user2644305
use setTag();
使用 setTag();
like this:
像这样:
@Override
public void onClick(View v) {
int tag = (Integer) v.getTag();
switch (tag) {
case 1:
System.out.println("button1 click");
break;
case 2:
System.out.println("button2 click");
break;
}
}
回答by DonSteep
In addition to Cristian C's answer (sorry, I do not have the ability to make comments), if you make one handler for both buttons, you may directly compare v to b1 and b2, or if you want to compare by the ID, you do not need to cast v to Button (View has getId() method, too), and that way there is no worry of cast exception.
除了Cristian C的回答(对不起,我没有能力发表评论),如果你为两个按钮制作一个处理程序,你可以直接将v与b1和b2进行比较,或者如果你想通过ID进行比较,你不需要将 v 转换为 Button (View 也有 getId() 方法),这样就不用担心转换异常。
回答by Tai Nguyen
Button mybutton = new Button(ViewPagerSample.this);
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});