Android - multiple OnClickListener?

前端 未结 5 2272
-上瘾入骨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: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
            }
        }
    

提交回复
热议问题