是否可以更改xml按钮上的文本?

时间:2011-10-29 09:50:07

标签: android xml button

如果我在名为test.xml的文件中有一个xml按钮,如下所示:

  <Button
  android:id="@+id/newgame"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="New Game"
  />

是否可以将“新游戏”文字更改为其他内容?我必须多次这样做。 最大的问题是:我必须从java文件中做到这一点!这甚至可能吗?

2 个答案:

答案 0 :(得分:5)

1)获取对Button的引用

2)使用setText方法为文本提供新值

Button button = (Button)findViewById(R.id.newgame);
button.setText("Something Else");

答案 1 :(得分:0)

绝对哥们,这类问题不应该问智能手机,你可以通过更新xml以及java编码来做到这一点 恩.... Button buttontext =(Button)findViewById(R.id.button1); button.setText(“这是新文本”); 或者在xml ..

在xml中,它将是静态的,使其成为动态,在java类中设置文本。 感谢