How to pass parameters to OnClickListener?

后端 未结 9 2218
南旧
南旧 2020-11-29 20:59

How can i pass parameter to an OnClickListener() ?

Got my Listener:

   OnClickListener myListener = new OnClickListener()
   {

     @Override
     p         


        
相关标签:
9条回答
  • 2020-11-29 21:21

    Another solution for that issue, you can create a regular method and pass to it the View you want to add the onClickListener to it, and pass the parameters you want to use along with it:

    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)
    
               }
        });
    }
    
    0 讨论(0)
  • 2020-11-29 21:26

    use implements OnClickListener like below code

    public class Test extends Activity implements OnClickListener{
        Button btn1;
        Button btn2;
        Button btn3;
        Button btn4;
        Button btn5;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            //initialize btn...~~~
    
            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
            btn5.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            switch(view.getId()) {
                case will be determine by Button's Id (R.id.blablabla)
            }
    }
    
    0 讨论(0)
  • 2020-11-29 21:27

    I know this is a late answer but hopefully it can help someone. None of the existing answers worked in my situation but I ended up using the setTag feature of an image that was acting like a button. My account info was in a global member variable that was set up when the activity started.

    In this case I am setting up a table with each row being an account. The account details are shown when the image is clicked (the image is just an info icon).
    Thus:

    // 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);
    
    0 讨论(0)
  • 2020-11-29 21:28

    Use your own custom 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
         }
    
      };
    
    0 讨论(0)
  • 2020-11-29 21:35

    If you have static content associated with the view, another option is to use a HashMap to map the view id to the content.

    private static Map<Integer, String> idToStringMap = new HashMap<Integer, String>();
    

    Initialize the map in onCreate:

    idToStringMap.put(R.id.button1, "hello");
    idToStringMap.put(R.id.button2, "world");
    // ...
    

    Then get the value from the view id in onClick:

    public void onClick(View v) {
    
        String myString = idToStringMap.get(v.getId());
    }
    

    This is a little cleaner than using a long set of switch cases.

    0 讨论(0)
  • 2020-11-29 21:40

    In Kotlin :

    Here i'm passing a view as a parameter onClick of Another view

    define your Handler class object first in xml layout like this:

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:bind="http://schemas.android.com/tools">
        <data>
            <import type="android.view.View" />
            <variable
                name="handler"
                type="com.demoapp.controllers.Handler" />
       </data>
    

    and define button which perform click functionality.

    <Button
      android:id="@+id/button2"
      android:onClick="@{(view) -> handler.getbuttonClickEvent(textViewId)}"
      .../>
    

    and your text view like this: remember your TextView id will be converted like textViewId

    <TextView
            android:id="@+id/text_view_id"
            ... />
    

    final set up method in your Handler class

    fun getbuttonClickEvent(view: TextView){
            view.text="Hello World"
        }
    
    0 讨论(0)
提交回复
热议问题