【问题标题】:what is better way to organize onclicklistener? [duplicate]组织 onclicklistener 的更好方法是什么? [复制]
【发布时间】:2016-01-28 04:48:59
【问题描述】:

当您在视图中有许多按钮并且所有按钮都有侦听器时。你的主要活动变脏了。 有人知道如何组织听众吗? 目前我使用这种方式并实现onClickListener。

    spotify =(Button)findViewById(R.id.spotifyBtn);
    superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
    libraryBtn = (Button) findViewById(R.id.libraryBtn);
    buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
    capstoneBtn= (Button) findViewById(R.id.capstoneApp);


    spotify.setOnClickListener(this);
    superDuoBtn.setOnClickListener(this);
    libraryBtn.setOnClickListener(this);
    buildBiggerBtn.setOnClickListener(this);
    capstoneBtn.setOnClickListener(this);

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以设置属性:

    android:onClick="buttonClicked"
    

    在每个按钮的 xml 文件中,并在 java 代码中使用它:

    public void buttonClicked(View view) {
    
            if (view.getId() == R.id.button1) {
                    // button1 action
                } else if (view.getId() == R.id.button2) {
                    //button2 action
                } else if (view.getId() == R.id.button3){
                    //button3 action
                }
        }
    

    【讨论】:

    • 我不知道我可以选择这种方式,但我更喜欢 onClick override one 不过谢谢
    【解决方案2】:

    您可以使用 swith case 为多个按钮实现 onclicklistner

    @Override
    public void onClick(View v) {
    
        switch (v.getId()) {
    
        case R.id.firstButton:
            // do your code
            break;
    
        case R.id.secButton:
            // do your code
            break;
    
        case R.id.thirdButton:
            // do your code
            break;
    
         ......
    
        default:
            break;
        }
    
    }
    

    【讨论】:

    • 谢谢,但我就是这样做的。
    • 那您还需要什么......它在工作吗??
    【解决方案3】:

    是的...这是使用多个 onClickListener 的最佳方式。

    spotify =(Button)findViewById(R.id.spotifyBtn);
        superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
        libraryBtn = (Button) findViewById(R.id.libraryBtn);
        buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
        capstoneBtn= (Button) findViewById(R.id.capstoneApp);
    
    
    
       spotify.setOnClickListener(this);
        superDuoBtn.setOnClickListener(this);
        libraryBtn.setOnClickListener(this);
        buildBiggerBtn.setOnClickListener(this);
        capstoneBtn.setOnClickListener(this);
    
    
    
    @Override
        public void onClick(View v) {
    
            Intent intent = null;
    
            switch (v.getId()) {
                case R.id.spotifyBtn:
                    intent = new Intent(this, SimpleSingleExample.class);
                    break;
    
                case R.id.superDuoBtn:
                    intent = new Intent(this, CustomExample.class);
                    break;
    
                case R.id.libraryBtn:
                    intent = new Intent(this, SequenceExample.class);
                    break;
    
                case R.id.buildItBiggerBtn:
    
                    Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
                    break;
            }
    
            if(intent!=null){
                startActivity(intent);
            }
        }
    

    【讨论】:

    • 我想除了这个..Tnx没有别的办法
    • 如果你觉得这有帮助,请接受我的回答。
    【解决方案4】:

    如果你想要更好的方法而不是使用 Android Annotations,它简单实用,you can find here

    【讨论】:

      【解决方案5】:

      将这些 View 对象引用添加到某种类型的列表,使用 for-each 循环遍历它,然后在每个元素上调用 setOnClickListener,这会将这些行减少到仅 2 行。

      ArrayList <View> list = new ArrayList <>(spotify,superDuoBtn,libraryBtn, buildBiggerBtn, capstoneBtn);
      for (View view : list) {
          view.setOnClickListener(this);
      }
      

      【讨论】:

        【解决方案6】:

        解决单个问题的替代方法最明显的例子似乎是您可以处理按钮单击的各种方法。据我所知,有四种不同的方法可以添加侦听器来处理按钮点击。如果您知道其他方式,请发表评论并与我们分享。

        Xml

          <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                >
                <Button android:text="Inner Class (btn1)" android:id="@+id/Button01"
                    android:layout_width="fill_parent" android:layout_height="wrap_content">
                </Button>
                <Button android:text="Anonymous Inner Class (btn2)"
                    android:id="@+id/Button02" android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                <Button android:text="Implementing an Interface (btn3)"
                    android:id="@+id/Button03" android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                <Button android:text="Calling From XML Layout (btn4)"
                    android:id="@+id/Button04" android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:onClick="btn4Listener">
                 </Button>
            </LinearLayout>
        

        在 MainActivity 中

        import android.app.Activity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.Toast;
        
        public class Main extends Activity implements View.OnClickListener {
        
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
        
              //method 1 - uses an inner class named btn1Listener...
              Button btn1 = (Button)findViewById(R.id.Button01);
              btn1.setOnClickListener(btn1Listener);
        
        
              //method 2 - use an anonymous inner class as a listener...
              Button btn2 = (Button)findViewById(R.id.Button02);
              btn2.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      showToastMessage("You clicked btn2 - uses an anonymouse inner class");
                  }
              });
        
              //method 3 - note that this class implements
              //the View.OnClickListener interface
              //which means that we must implement the onClick()
              //method (which you'll find below)..
              Button btn3 = (Button)findViewById(R.id.Button03);
              btn3.setOnClickListener(this);
              //method 4 - look at the method btn4Listener() below       
          }
        
        
          //here's the inner class used as a listener for btn1...
        
          private View.OnClickListener btn1Listener = new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                showToastMessage("You clicked btn1 - uses an inner class named btn1Listener");
              }
          };
        
        
          //here's a method that you must have when your activity implements the
          //View.OnClickListener interface...
          @Override
          public void onClick(View v) {
              showToastMessage("you clicked on a btn3, which uses this Activity as the listener");
          }
          //here's the handler for btn4 (declared in the xml layout file)...
          //note: this method only works with android 2.1 (api level 7), it must be public and
          //must take a single parameter which is a View
        
          public void btn4Listener(View v) {
                  showToastMessage("You clicked btn4 - listener was set up in the XML layout");
          }
          private void showToastMessage(String msg){
              Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
              toast.show();
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-08-28
          • 2012-10-23
          • 2010-09-13
          • 1970-01-01
          • 1970-01-01
          • 2013-06-09
          • 2010-11-04
          • 1970-01-01
          • 2023-04-04
          相关资源
          最近更新 更多