[英]How can I change the text with a button click?
當我單擊一個按鈕時,我希望 fullscreen_content 中的字符串 @string/dummy_content 改變。 我怎樣才能做到這一點?
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">
按鈕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 功能:
public void printStarter(View view) {
}
嘗試這個..
您不能在運行時更改字符串資源
public void printStarter(View view) {
((TextView)findViewById(R.id.fullscreen_content)).setText("Your Text");
}
您需要獲取需要設置其文本的 TextView 的引用。 這是代碼:
public void printStarter(View view){
TextView text=(TextView)findViewById(R.id.fullscreen_content);
text.setText("Some text....");
}
您不能動態更改字符串資源,因為它是已編譯的資源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.