简体   繁体   English

每个类都必须具有自己的OnClickListener吗?

[英]Does each class have to have it's own OnClickListener?

Total Android beginner here... 总Android初学者在这里...

I have an Activity with an OnClick Listener as in this example: Multiple Buttons `OnClickListener()` android 我有一个带有OnClick监听器的活动,如本例所示: 多个按钮`OnClickListener()`android

And now I'm setting up a listener on Floating Action Button in a different activity. 现在,我在另一个活动中的“浮动操作按钮”上设置了一个侦听器。 What I'm not sure about is whether it is possible to use that same listener, or does each class have to have it's own? 我不确定的是是否可以使用相同的侦听器,还是每个类都必须拥有自己的侦听器?

ACTIVITY #1 // a regular activity
public class Requests extends AppCompatActivity implements View.OnClickListener {...}

ACTIVITY #2 // A RecyclerView, CardView type activity
public static class MyViewHolder extends RecyclerView.ViewHolder {...}

I'd really like to have one Listener to handle the cardview click events, as well as the Floating Action Button. 我真的很希望有一个侦听器来处理cardview点击事件,以及“浮动操作按钮”。

in general you can use the same listener for multiple buttons. 通常,您可以将同一监听器用于多个按钮。 if they are in different activities , you can write a seperate class that implements View.OnClickListener . 如果它们处于不同的活动中,则可以编写一个单独的类来实现View.OnClickListener。 Like this: 像这样:

public class MyButtonListener implements View.OnClickListener {

@Override
public void onClick(View v) {
  switch (v.getId()) {

    case R.id.oneButton:
        // do your code
        break;

    case R.id.twoButton:
        // do your code
        break;

    case R.id.threeButton:
        // do your code
        break;// default method for handling onClick Events..
}

} }

Then you just have to set your button listener like button.setOnClicklistener(new MyOnClickListener()); 然后,您只需要像button.setOnClicklistener(new MyOnClickListener());那样设置按钮侦听器button.setOnClicklistener(new MyOnClickListener());

If your buttons are all doing the same action, you wont need the switch-case block. 如果您的按钮都执行相同的操作,则不需要开关盒块。

Creating a seperate listener class is not a bad idea at all BUT: you should try to implement one for each activty to keep an overview over your button actions. 创建一个单独的监听器类并不是一个坏主意,但您应该尝试为每个活动实现一个监听类,以保持对按钮操作的了解。

You can use below code snipet mentioned in the link below 您可以使用以下链接中提到的以下代码片段

Handle click item in Recycleview 处理Recycleview中的单击项

In that onClick and onItemClick Override listener you can implement both Floating Action Button and cardview click events position wise 在该onClick和onItemClick覆盖侦听器中,您可以在位置上实现浮动操作按钮和cardview点击事件

To perform same operation on click event of different view or viewGroup implement an anonymous class for OnClickListener as 要对不同视图或viewGroup的click事件执行相同的操作,请为OnClickListener实现一个匿名类,如下所示:

View.OnClickListener mOnClickListener= new View.OnClickListener() {  
 @Override public void onClick(View v) {    /*do your code */ }};   

or call it in your activity as 或在您的活动中将其称为

 mbutton.setOnClickListener(mOnClickListener);   
    mcardView.setOnClickListener(mOnClickListener);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个类默认都有自己的线程吗? - Does each class have its own Thread by default? 如果我的程序中有多个线程,从技术上讲每个线程是否都有自己的堆栈? 每个线程都有自己的堆吗? - If I have multiple threads in my program, does each thread technically have it's own stack? Does each thread have it's own heap? 创建按钮,但让每个按钮都有自己的变量名 - Creating Buttons but have each Button have it's own variable name 每个JSpinner对象是否都有自己的Model对象? - Does each JSpinner object have its own Model object? 每个 Solr 核心都有自己的类加载器吗? - Does each Solr core have its own classloader? 有没有一种方法可以调用一个父类构造函数,该构造函数从一个不具有自己构造函数的子类中获取参数? - Is there a way I can call a parent class constructor that takes parameters from a child class that does not have a constructor of it's own? 是否可以扩展 Button class 并让它实现自己的 OnClick 监听器? - Is it possible to extend the Button class and have it implement it's own OnClick listener? 我的Xposed模块是否有自己的Context? - Does my Xposed module have it's own Context? Java是否拥有自己的内置JSON解析器 - Does Java have it's own built-in JSON parser eclipse是否有自己的JDK? 还是在我的计算机上使用了JDK? - does eclipse have it's own JDK? or it uses the JDK on my computer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM