Get text from pressed button

前端 未结 6 1556
栀梦
栀梦 2020-12-01 03:08

How can I get the text from a pressed button? (Android)

I can get the text from a button:

String buttonText = button.getText();

I c

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 03:50

    The view you get passed in on onClick() is the Button you are looking for.

    public void onClick(View v) {
        // 1) Possibly check for instance of first 
        Button b = (Button)v;
        String buttonText = b.getText().toString();
    }
    

    1) If you are using a non-anonymous class as onClickListener, you may want to check for the type of the view before casting it, as it may be something different than a Button.

提交回复
热议问题