2014-02-11 25 views
0

我最近開始使用ADT(我還不熟悉java,我熟悉C++)。Android開發工具包 - 更改字符串的值

我添加了一個TextView其中有一個字符串作爲其文本:

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button1" 
    android:layout_marginBottom="35dp" 
    android:layout_toRightOf="@+id/textView1" 
    android:text="@string/button_clicked" /> 

<string name="button_clicked">0</string> 

(在文件activity_main.xml中佈局和string.xml的字符串)

現在我的問題是改變文本TextView。但我不想直接改變它,我想改變字符串。 (這是由於onClick事件的一個按鈕)

到現在爲止我還沒有制定出如何做到這一點,以及如何將int的值以某種方式轉換成該字符串(我希望它計數/顯示此整數的值)。

你們能幫我嗎? :)

+0

使用'setText'方法和'String.valueOf'? –

+0

tv.setText(getText(R.string.button_clicked));有些事情是這樣的我們想要不一樣 – Developer

回答

2

如果你只是想設置一個String作爲文本:

myTextView.setText(myString); 

對於整數,使用:

myTextView.setText(Integer.toString(integer)); 

對於字符串資源,用途:

myTextView.setText(R.string.my_string_res); 
+0

謝謝! 這有幫助! :) – hinde

+1

那你應該接受他的回答:) – Fraggles