的onclick在Android的监听器监听器、onclick、Android

2023-09-13 01:05:50 作者:女汉子也有颗玻璃心

我用了两个图像按钮,下一步和上一步,我用onclick事件的按钮,我想这图像按钮火的onclick并运行特定功能的onclick事件旁边或后面我将如何得到该图像按钮火灾或的onclick事件在运行时

I used two image button for Next and Back and i used onclick event for those button i want to which image button fire on onclick and run particular function for next or back in onclick event how will i get which image button fire or onclick event at runtime

推荐答案

您可以使用匿名内部类编写一个onClick函数的每个按钮。

You can use anonymous inner classes to write an onClick function for each button.

Button button1 = getMyButton();
button1.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      // button 1 was clicked!
   }
  });
Button button2 = getMyButton();
button2.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      // button 2 was clicked!
   }
  });

随着康斯坦丁提到的,你也可以使用传入的查看和切换上的ID。不过,我觉得这有点混乱,如果你结了许多可点击的东西。

As Konstantin mentioned, you can also use the passed in View and switch on the id. However, I find that a bit messier if you end up with lots of clickable things.