Android - multiple OnClickListener?

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

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

    Example :

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

提交回复
热议问题