Android 程序运行正常,但eclipse显示textview警告

Android 程序运行正常,但eclipse显示textview警告,android,Android,我想知道为什么eclipse会在下面的xml代码中显示警告“[I18N]硬编码字符串“TextView”,应该使用@string resource”。实际上,我正在尝试将用户在活动的编辑文本中写入的文本添加到此当前活动 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android

我想知道为什么eclipse会在下面的xml代码中显示警告“[I18N]硬编码字符串“TextView”,应该使用@string resource”。实际上,我正在尝试将用户在活动的编辑文本中写入的文本添加到此当前活动

<?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>

正如它所说,您使用的是“硬编码”字符串,其效率低于使用
字符串资源的效率。简单删除

android:text="TextView"
如果你不想看到警告。如果需要,则忽略警告或将其添加到
字符串资源
文件中。不需要
Text
属性。如果您需要用户输入,则应将其更改为
EditText
,除非您有理由使用
TextView

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

然后,如果您希望它在
视图中显示诸如“enterinput here”之类的内容,那么您可以添加
android:hint“Text to display”
。但是,如果您不将其添加到
strings.xml
并使用
android:hint=“@string/nameInStringsFile”
,则会得到相同的警告


但这些警告正是如此。建议可能更有效的方法或方法来实现您正在做的任何事情。

将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”。Android的最佳做法是将所有字符串放在创建的res/values文件夹的Strings.xml文件中。如果资源文件中有字符串,则可以使用语法“@string/string_name”从布局文件中引用该字符串。

您收到警告的原因是,您试图硬编码一个字符串,但由于可能存在冗余,该字符串在安卓编程中不符合常规:

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

您应该在…/res/values/strings.xml文件中创建对字符串的引用,如下所示:

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

。。并在strings.xml文件中定义它:

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

希望这能有所帮助。

这个警告还有什么不清楚的地方?将所有硬编码字符串保存在一个位置(作为资源)被认为是一种很好的做法,因此Eclipse警告您没有这样做。别费心说太多,这只是一个警告。或者,您可以删除导致警告的行,因为您不使用硬编码值。