【问题标题】:Delegate(common) click listener in AndroidAndroid中的委托(通用)点击监听器
【发布时间】:2018-03-25 22:25:43
【问题描述】:

假设我有几个按钮,它们的点击由一个通用处理程序处理,有没有一种方法可以定义一次侦听器而不为每个按钮定义 onClick 属性?

<Button android:onClick="handler" />

也许是浏览器或某些拦截器中的父委托.....假设您有 50 个按钮,您在点击时明确声明每个按钮???

【问题讨论】:

  • 使用 onClick(View v) 方法
  • 您可以为所有按钮提供相同的点击处理程序,然后在 onClick() 方法中,只需使用 switch case 进行各种不同的操作。

标签: android button click touch listener


【解决方案1】:

最好的方法:让你的活动实现View.OnClickListener,然后像这样编写你的onClick方法:

public void onClick(View v) {
    final int id = v.getId();
    switch (id) {
    case R.id.button1:
        // your code for button1 here
        break;
    case R.id.button2:
        // your code for button2 here
        break;
    // even more buttons here
    }
}

然后,在您的 XML 布局文件中,您可以直接使用属性android:onClick: 设置点击监听器

<Button
    android:id="@+id/button1"
    android:onClick="onClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />

【讨论】:

  • 但这就是我想避免为每个按钮明确声明onClick....
  • 好吧,试试普通的onClickListener。并将该侦听器分配给您的按钮。
【解决方案2】:

是的,它可以在活动级别以编程方式实现。所以不需要在 XML 中定义“onClick”属性。

您定义了一个 View.OnClickListener 类。例如

View.OnClickListener buttonListener = new View.OnClickListener() {
         public void onClick(View v) {
             // here you can define different code for different buttons
         };

然后您将此侦听器附加到一个位置上的所有按钮

final Button button = findViewById(R.id.button_id);
     button.setOnClickListener(buttonListener);

【讨论】:

    【解决方案3】:

    是的,您可以这样做。

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    List<Button> buttons;
    ViewGroup parentView;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttons = new ArrayList<>();
        parentView = (ViewGroup) findViewById(R.id.cord);
    
    
        //Replace the above line with the container view i.e.
        //Replace  parentView = (ViewGroup) findViewById(R.id.cord);
        // With parentView = (ViewGroup) findViewById(R.id.<YOUR MAIN VIEW/PARENT CONTAINER>);
        //R.id.cord is R.id.<YOUR MAIN VIEW/PARENT CONTAINER> for me because my Relative Layout 
        // container's name is cord.
    
    
        for(int i=0; i < parentView.getChildCount(); i++) {
    
            childView = parentView.getChildAt(i);
            //The if may change there are other options try them too.
            if(childView.getClass().getName().substring(35).equals("Button")){
    
    
                buttons.add((Button)parentView.getChildAt(i));
            }
            /*
            //Else part optional 
            //Remove comment to use
            else
                Log.e("Not","A Button");*/
        }
    
        for (int i =0;i<buttons.size();i++){
            buttons.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Do the work here
                    Log.d("Button Clicked",Integer.toString(view.getId()));
                }
            });
        }
    
    
    
    
    
        }
    }
    

    这是一种解决问题的方法...

    【讨论】:

    • 如果你知道要使用多少个按钮,你可以使用数组而不是列表。
    【解决方案4】:

    这是我最终得出的解决方案,它仅适用于 touchables,例如 TextView 不是可触摸的:

    在java中:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        final MainActivity activity = this;
        /* Any kind of ViewGroup that is 
           parent of the common listener views. In the example it is the rootView */
        View rootView = this.getWindow().getDecorView().getRootView();
    
        // Buttons are touchables
        ArrayList<View> touchables = rootView.getTouchables();
    
        // Define the common on click listener for the buttons
        View.OnClickListener listener = new View.OnClickListener() {
            public final void onClick(View view) {
                activity.onBtnClick( (Button) view );
            }
        };
    
        // Iterate trough touchables and if the View is a button assign the listener
        for (View v: touchables) {
            // TODO: omit button if that common listener isn't meant for it
            if(v instanceof Button) {
                v.setOnClickListener(listener);
            }
        }
    }
    
    // The listener callback
    protected final void onBtnClick(Button btn) {
        String btnStringId = btn.getResources().getResourceEntryName(btn.getId());
    }
    

    在 Kotlin 中:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        /* Any kind of ViewGroup that is 
           parent of the common listener views. In the example it is the rootView */
        val rootView = window.decorView.rootView
        val touchables = rootView.touchables
        val listener = View.OnClickListener {
            onBtnClick(it as Button)
        }
        touchables.forEach { if (it is Button) {
            it.setOnClickListener(listener)
        }}
    }
    
    protected fun onBtnClick(btn: Button) {
        var btnStringId = btn.getResources().getResourceEntryName(btn.id)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 2013-05-28
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      相关资源
      最近更新 更多