Easier way to get view's Id (string) by its Id (int)

前端 未结 8 1335
暗喜
暗喜 2020-12-01 01:39

I\'m new with android and java, so sorry if it\'s a too basic question but I\'ve tried to find a solution in the forums and google and I couldn\'t.

I have 24 buttons

8条回答
  •  执笔经年
    2020-12-01 02:21

    You can check id of each button such way:

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.add_04:
            Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
            break;
        case R.id.add_05:
            Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
            break;
        }
    }
    

提交回复
热议问题