2017-03-28 36 views
-6

我是Android編程的新手,我想使用像這樣的方法爲TextView設置文本。如何從字符串設置TextView的文本

textView.setText(freezed_string); 

freezed_string名是固定的,我想改變它的值當點擊不同的四個按鈕。

Button1的被點擊時,的freezed_string變化值成一個

當點擊Button2的freezed_string成兩個的變化值。

按鈕3被點擊,更改值爲freezed_string爲三。

按鈕4被點擊,更改值爲freezed_string爲四。

希望你明白我的想法,請指出解決方案對我和它的高度讚賞!

+1

您的問題還不夠清楚,請閱讀以下問題:[問]&[mcve]&[help/on-topic],以增加您獲得問題答案的機會! –

+0

添加你的java和xml文件或者你試過的任何東西。它不是做網站的作業。 –

+0

難道你不能只是添加一個'OnClickListener'到每個按鈕來設置'TextView'文本的數字。 – JediBurrell

回答

1

做這樣的事情

String freezed_string_value = "one"; 

Button2 = (Button)findViewById(R.id.Button2); 
Button2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       freezed_string_value = "two" 
      } 
     }); 

,要所有按鈕和數值

+0

以上答案是正確的,但我們必須在freezed_string_value =「two」下添加一行,並且將該字符串設置爲TextView的文本以更新文本。像這樣: tx.setText(freezed_string_value); –

2

您創建了一個String freezedString = "<some default value>";並將TextView設置爲已顯示的那個字符串。 當按鈕被按下變化freezedString到任何你想要的值落實View.onClickListener界面爲你的活動(每個按鈕。

這可以在計算器上輕鬆地擡起頭來。我會建議使用本網站作爲一種資源。

CHECK IT OUT

0

正如你嘩嘩 注意在XML

android:id="@+id/one_button" 
    android:onClick="click" 

,你會發現在https://developer.android.com/reference/android/widget/Button.html

答案請嘗試以下操作:

main_activity.xml

<Button 
    android:id="@+id/one_button" 
    android:onClick="click" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button1" /> 

<Button 
    android:id="@+id/two_button" 
    android:onClick="click" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button2" /> 

<Button 
    android:id="@+id/three_button" 
    android:onClick="click" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button3" /> 

<Button 
    android:id="@+id/four_button" 
    android:onClick="click" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button4" /> 
<TextView 
    android:id="@+id/show_text" 
    android:layout_gravity="center" 
    android:textSize="50dp" 
    android:text="DEMO" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
MainActivity

。java的

public class MainActivity extends AppCompatActivity { 

private TextView mShowText; 
private String freezed_string; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    initView(); 

} 

private void initView() { 
    mShowText = (TextView) findViewById(R.id.show_text); 
} 


public void click(View view) { 
    switch (view.getId()) { 
     case R.id.one_button: 
      freezed_string = "one"; 
      mShowText.setText(freezed_string); 
      break; 
     case R.id.two_button: 
      freezed_string = "two"; 
      mShowText.setText(freezed_string); 
      break; 
     case R.id.three_button: 
      freezed_string = "three"; 
      mShowText.setText(freezed_string); 
      break; 
     case R.id.four_button: 
      freezed_string = "four"; 
      mShowText.setText(freezed_string); 
      break; 

    } 
} 

}如果你想直接將該值設置爲你的TextView 然後鍵入textView.setText("two");代替freezed_string_value = "two"

重複

+0

以上答案是正確的,幷包含所有必需的信息! –

相關問題