Java 如何通过单击按钮更改文本?安卓工作室xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22573299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i change the text with a button click? android studio xml
提问by lehermj
when i click a button i want the the string @string/dummy_content in fullscreen_content to change, how can i do that?
当我单击一个按钮时,我希望 fullscreen_content 中的字符串 @string/dummy_content 改变,我该怎么做?
xml:
xml:
<TextView android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:textColor="#2f4b66"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:text="@string/dummy_content">
button xml:
按钮xml:
<Button android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button"
android:clickable="true"
android:onClick="printStarter"
/>
java function i would like to activate:
我想激活的java函数:
public void printStarter(View view) {
}
采纳答案by Hariharan
Try this..
尝试这个..
You can't change string resource at the run time
您不能在运行时更改字符串资源
public void printStarter(View view) {
((TextView)findViewById(R.id.fullscreen_content)).setText("Your Text");
}
回答by Tanuj Wadhwa
You need to get the reference of the TextView whose text you need to set. Here is the code:
您需要获取需要设置其文本的 TextView 的引用。这是代码:
public void printStarter(View view){
TextView text=(TextView)findViewById(R.id.fullscreen_content);
text.setText("Some text....");
}
You can't change string resource dynamically since it's a compiled resource.
您不能动态更改字符串资源,因为它是已编译的资源。