Android中的onclick监听器

8

我使用了两个图像按钮来进行“下一页”和“上一页”的操作,我为这些按钮使用了onclick事件。在onclick事件中,我需要知道哪个图像按钮被触发,并运行相应的下一页或上一页函数。如何在运行时获取哪个图像按钮触发了onclick事件?

4个回答

12
使用 View.getId() 方法来区分不同的视图并触发 onClick 事件。
@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.download:
    //code..
    break;
    case R.id.play:
    //code..
    break;
    case R.id.pause:
        //code..
    break;
    default:
        //code..
    break;
    }
}

嗨,我正在使用以下概念: imgvw_back.setOnClickListener(this) imgvw.setOnClickListener(this); static int i=10; @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.back: Log.v("back",""+i--); break; case R.id.forward: Log.v("next",""+i++); break; }}但是没有起作用,我不知道为什么不能运行?大多数情况下在日志中显示下一个单词。 - sivaraj
你尝试在onClick方法上设置断点了吗?它是否运行? - Konstantin Burov

12

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

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!
   }
  });

正如Konstantin所提到的那样,您也可以使用传入的View并切换ID。 但是,如果您最终有很多可单击的内容,我认为这会变得有些混乱。


我非常确定这是首选方法,除非你有很多按钮。 - Falmarri

0

将 onClickListener 设置为文本:前往 activity_main.xml 并添加:

<androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="12dp"
        android:textSize="18sp"
        tools:text="My Github"
        android:textColor="@color/black" />

MainActivity.java 文件中添加:
public class MainActivity extends AppCompatActivity{

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
AppCompatTextView textView = findViewById(R.id.textview);
textView.setOnClickListener(view-> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/fekri86114"))));
           
}
}

0

这可以通过在XML中为ImageButton声明一个唯一的ID来简单完成,如下所示

<ImageButton 
     android:id="@+id/backButton"
     .../>
<ImageButton
     android:id="@+id/nextButton"
     .../>

然后在JAVA中,如果您使用

绑定

即通过为页面使用ViewModel,那么您可以在onCreate()方法中使用以下代码,并且必须将您的代码放置在View root = binding.getRoot();return root;之间。

View root = binding.getRoot();

ImageButton backBtn = binding.backButton;
ImageButton nextBtn = binding.nextButton;

//Now Comes the OnClickEvent using Lambda a easiest way

backBtn.setOnClickListener(a -> 
{
    //Your Handler Code for Back Button
});

nextBtn.setOnClickListener(b -> 
{
    //Your Handler Code for Next Button
});

return root;

然后是

正常的

ImageButton backBtn = findViewById(R.id.backButton);
ImageButton nextBtn = findViewById(R.id.nextButton);

//onClickListeners
backBtn.setOnClickListener(a -> 
{
    //Your Handler Code for Back Button
});    
nextBtn.setOnClickListener(b -> 
{
    //Your Handler Code for Next Button
});

这个Lambda表达式可以通过两种方式完成,分别为:

方法1

view.setOnCLickListener(method1 -> 
{
    //Your Handler Code
});

方法二

如果您已经定义了一个函数并且要设置这个方法,则可以使用以下步骤:

view.setOnClickListener(method2 -> functionName(); );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接