什么是组织onclicklistener的更好方法?

时间:2016-01-28 04:48:59

标签: android

当视图中有许多按钮且所有按钮都有监听器时。你的主要活动变脏了。 任何人都知道如何组织听众? 目前我使用这种方式并实现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);

6 个答案:

答案 0 :(得分:2)

您可以设置属性:

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
            }
    }

答案 1 :(得分:1)

您可以使用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;
    }

}

答案 2 :(得分:1)

Ya ...这是使用多个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);
        }
    }

答案 3 :(得分:1)

如果您想要比使用 Android Annotations 更好的方式,那么它简单实用,you can find here

答案 4 :(得分:1)

将那些View对象引用添加到某种类型的列表中,在for-each循环中迭代它,然后在每个元素上调用setOnClickListener,这将为这些行减少到只有2行。

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: EISDIR: illegal operation on a directory, read
    at Error (native)

答案 5 :(得分:0)

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

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