Android widget: How to change the text of a button

前端 未结 7 1511
野性不改
野性不改 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 07:14

    I had a button in my layout.xml that was defined as a View as in:

    final View myButton = findViewById(R.id.button1);
    

    I was not able to change the text on it until I also defined it as a button:

    final View vButton = findViewById(R.id.button1);
    final Button bButton = (Button) findViewById(R.id.button1);
    

    When I needed to change the text, I used the bButton.setText("Some Text"); and when I wanted to alter the view, I used the vButton.

    Worked great!

    0 讨论(0)
提交回复
热议问题