如何将参数传递给OnClickListener?

Ada*_*gyi 58 android onclicklistener

如何将参数传递给OnClickListener()?

听了我的听众:

   OnClickListener myListener = new OnClickListener()
   {

     @Override
     public void onClick(View v)
     {
         //I want to reach params here

     }

  };
Run Code Online (Sandbox Code Playgroud)

我有12个按钮,我不想为他们写12个听众,将字符串传递给他们会很棒,他们可以做完全不同的事情.

She*_*tib 152

使用您自己的自定义OnClickListener

public class MyLovelyOnClickListener implements OnClickListener
   {

     int myLovelyVariable;
     public MyLovelyOnClickListener(int myLovelyVariable) {
          this.myLovelyVariable = myLovelyVariable;
     }

     @Override
     public void onClick(View v)
     {
         //read your lovely variable
     }

  };
Run Code Online (Sandbox Code Playgroud)


Muh*_*aat 23

针对该问题的另一个解决方案是,您可以创建常规方法并将其View添加onClickListener到其中,然后传递要与其一起使用的参数:

Button b1 = new Button();
String something = "something";
Button b2 = new Button();
String anotherSomething = "anotherSomething";
setOnClick(b1, something);
setOnClick(b2, anotherSomething);

private void setOnClick(final Button btn, final String str){
    btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                  // Do whatever you want(str can be used here)

           }
    });
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*zon 7

  1. 让您的活动实现View.OnClickListener
  2. 将您的按钮注册到听众
  3. 检查onClick中单击了哪个按钮
  4. 根据单击的按钮处理字符串

    public class _Test2Activity extends Activity implements OnClickListener {
    
       private Button button1;
       private Button button2;
       private Button button3;
    
       private String myString;
    
       @Override
       public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
    
         button1 = (Button)findViewById(R.id.button1);
         button2 = (Button)findViewById(R.id.button2);
         button3 = (Button)findViewById(R.id.button3);
    
         button1.setOnClickListener(this);
         button2.setOnClickListener(this);
         button3.setOnClickListener(this);   
    
         myString = "This is a string";
    
       } 
    
       @Override
       public void onClick(View v) {
           if(v==button1) {
              //do something with myString
           } else if(v==button2) {
              //do something with myString
           } else if (v==button3) {
              //do something with myString
           }
       }
    }
    
    Run Code Online (Sandbox Code Playgroud)


Gra*_*oid 7

我知道这是一个迟到的答案,但希望它可以帮助某人.现有的答案都没有在我的情况下起作用,但我最终使用的图像的setTag功能就像一个按钮.我的帐户信息位于活动开始时设置的全局成员变量中.

在这种情况下,我正在设置一个表,每行都是一个帐户.单击图像时会显示帐户详细信息(图像只是一个信息图标).
从而:

// prior code....
// NOTE: oneAccount is an object (AccountInfo) holding information about the account

// column with the "detail" arrow
image1 = new ImageView(this);
image1.setImageResource(R.drawable.info);
image1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
image1.setPadding(15, 1, 15, 1);
image1.setTag(oneAccount);
// add a listener to go to that account detail on a click
image1.setOnClickListener(new TextView.OnClickListener() {
    public void onClick(View v) {
    // change the color to signify a click
    v.setBackgroundColor(getResources().getColor(R.color.button_selected));


    // get what we need out of the tag
    AccountInfo thisAcct = (AccountInfo) v.getTag();

        // your code would do other stuff here

}
});

// add the image to the table row
tr1.addView(image1);
Run Code Online (Sandbox Code Playgroud)