How to determine which button pressed on android

前端 未结 5 708
闹比i
闹比i 2020-12-01 09:38

i need to know, how to recognize, which button is pressed. Like if i have two buttons ,say button 1 and button2,and both of them performing the same method, say method(),how

5条回答
  •  -上瘾入骨i
    2020-12-01 10:14

    Most ellegant pattern to follow:

    public void onClick(View v) {
    switch(v.getId())
    {
    case R.id.button_a_id:
    // handle button A click;
    break;
    case R.id.button_b_id:
    // handle button B click;
    break;
    default:
    throw new RuntimeException("Unknow button ID");
    }
    

    This way it's much simplier to debug it and makes sure you don't miss to handle any click.

提交回复
热议问题