2013-10-25 130 views
0

我正在開發eclipse仿真器「Nexus one」中的一個應用程序,我試圖將這些按鈕添加到格式中。當我添加按鈕他們很好。當我將我自己的文本添加到按鈕時,我收到硬編碼警告。所以我會繼續添加「@字符串/」來擺脫錯誤/警告,但問題在於「@ string /」顯示爲按鈕上的文本。硬編碼文本問題

- 所以我怎麼做的按鈕說只是個人資料和日曆沒有錯誤,或@字符串/

我對如何解決這一問題不知道。這裏是代碼和圖片:從您android:text屬性

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="@string/Profile" 
    android:onClick="profile" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/button1" 
    android:layout_marginTop="20dp" 
    android:text="@string/Calendar" 
    android:onClick="calendar"/> 

enter image description here

+5

您是否在strings.xml中定義了包含鍵「Profile」和「Calendar」的字符串? – for3st

+0

謝謝!我不明白爲什麼這會立即下降。 – Aaron

+0

它可能被低估的原因是,這是一個非常基本的錯誤,一個閱讀幾乎所有abitratry android tut都可以解決的錯誤 - 我想SO看起來更像是一個高級Q&A – for3st

回答

1

簡單刪除@string/。因此,每個按鈕看起來像這樣:

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Profile" 
    android:onClick="profile" /> 

@string/my_string_name用於引用在strings.xml中資源文件中定義的字符串。

解決這些錯誤的另一種(和「正確的」)方法是用XML定義這些字符串。 如果您編輯strings.xml並添加諸如<string name="Profile">Profile</string>這樣的行,這些錯誤將會消失,並且您將會有一個能夠輕鬆轉換爲其他語言的應用程序。