2013-07-15 22 views
0

我想知道爲什麼日食顯示警告「[I18N]硬編碼字符串」TextView「,應該使用@string資源」在下面的xml代碼。其實我試圖將活動中的編輯文本中用戶寫入的文本發送到當前活動。程序工作正常,但日食顯示警告textview

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:layout_weight="0.01" 
      android:text="TextView" /> 

    </LinearLayout> 
+2

關於此警告的含義尚不清楚?將所有硬編碼字符串保存在一個地方(作爲資源)被認爲是一種很好的做法,因此Eclipse會警告您沒有。不要打擾太多,這只是一個警告。或者,您可以刪除導致該警告的行,因爲您無論如何都不使用硬編碼值。 –

回答

0

,因爲它說,你使用的是「硬編碼」字符串,它比使用String resource效率較低。只需刪除

android:text="TextView" 

如果您不希望警告顯示。如果您想要,可以忽略該警告或將其添加到String resource文件中。 Text屬性是不需要的。如果你期待用戶輸入,那麼你應該將其更改爲EditText無論如何,除非你有,如果你想讓它在View顯示一些諸如「在這裏輸入的輸入」,那麼您使用TextView

<EditText 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.01" /> 

然後一個理由可以添加android:hint"Text to display"。但是,如果您不將它添加到strings.xml並使用android:hint="@string/nameInStringsFile",這會給您相同的警告。

但這些警告就是這樣。提出可能更有效的方法或實施你正在做的任何方式。

0

更改XML以下刪除警告

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:layout_weight="0.01" /> 

    </LinearLayout> 

你看到警告的原因是因爲你已經設置的XML佈局文件中的文本爲「TextView的」。將所有字符串放入res/values文件夾中的strings.xml文件中,這是創建string resources的最佳實踐。在資源文件中有一個字符串,您可以使用語法「@ string/string_name」從佈局文件中引用它。

2

您收到一個警告的原因,是由於這樣的事實,你想硬編碼字符串,這是不是在Android的編程由於可能的冗餘好習慣:

<TextView 
     ... 
     android:text="TextView" /> 

你還是創建像這樣的字符串的引用在.../RES /價值/ strings.xml檔案:

<TextView 
     ... 
     android:text="@string/TextView" /> 

..並在strings.xml文件中定義它:

<string name="TextView">TextView</string> 

希望這會有所幫助。

相關問題