Android widget: How to change the text of a button

前端 未结 7 1567
野性不改
野性不改 2020-12-13 06:31

How can I change the text of an Android button widget within code and not the XML file?

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 06:54

    You can use the setText() method. Example:

    import android.widget.Button;
    
    Button p1_button = (Button)findViewById(R.id.Player1);
    p1_button.setText("Some text");
    

    Also, just as a point of reference, Button extends TextView, hence why you can use setText() just like with an ordinary TextView.

提交回复
热议问题