Android - multiple OnClickListener?

前端 未结 5 2315
-上瘾入骨i
-上瘾入骨i 2021-02-20 03:39

I have 4 images. We should be able to click on these images. I\'d like to know if I have to create 4 OnClickListener, or there is another way to do this properly?

相关标签:
5条回答
  • 2021-02-20 03:40

    To make any view listen to our action you have to attach listener to that view. So you need to attach four listeners. Attaching OnclickListener and writing implementation both are two different things

    0 讨论(0)
  • You can reuse your listener:

    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int which) {
                                   /*...*/
                               }
                            });
    
    phone.setOnClickListener(listener);
    bookings.setOnClickListener(listener);
    /*...*/
    
    0 讨论(0)
  • 2021-02-20 03:51

    You can just do it like this,

    phone.setOnClickListener(this);
    bookings.setOnClickListener(this);
    settings.setOnClickListener(this);
    pictures.setOnClickListener(this);
    

    And in the onClick() method,

     @Override
        public void onClick(View v) {
    
           if(v == phone){
                  // your stuff
            }
           else if(v == bookings){
                  // your stuff
            }
           else if(v == settings){
                  // your stuff
            }
           ese if(v == pictures){
                  // your stuff
            }
        }
    
    0 讨论(0)
  • 2021-02-20 03:56

    You can use/make your listener this way:-

    img1.setOnClickListener(imgClk);
    img2.setOnClickListener(imgClk);
    img3.setOnClickListener(imgClk);
    img4.setOnClickListener(imgClk);
    

    And then you have to create OnClickListener after onCreate/out side of onCreate()

        public OnClickListener imgClk = new OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                  switch(v.getId()){
                    case R.id.img1:
                     //write your code here
                      break;
    
                   case R.id.img2:
                     //write your code here
                      break;
    
                   case R.id.img3:
                     //write your code here
                      break;
    
                   case R.id.img4:
                     //write your code here
                      break;
               }
           };
    

    I hope it helps you.

    0 讨论(0)
  • 2021-02-20 03:58

    I would suggest using android:onClick for more readable code.

    Example :

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

    Then in your activity class add the onClick method.

    public void onClick(View v) {
          switch(v.getId()){
               case R.id.myButton:
                       //Your logic goes here...
                       break;
    
               default:
                       break;
          }
    }
    
    0 讨论(0)
提交回复
热议问题