【问题标题】:Error Comparing text of editText比较editText的文本时出错
【发布时间】:2013-10-21 02:11:11
【问题描述】:

此代码尝试捕获 EditText 的字符串并将其与数组的字符串进行比较。 代码没有错误,但是当我单击按钮时没有任何反应(甚至没有显示吐司),我做错了什么? 谢谢

这是数组数组。 列表.xml

<resources>
        <string-array name="array1">
            <item name="number">1</item>
            <item name="name">first</item>
        </string-array>
        <string-array name="array2">
            <item name="number">2</item>
            <item name="name">second</item>
        </string-array>

    <array name="list">
        <item>@array/array1</item>
        <item>@array/array2</item>
    </array>
</resources>

这是主xml文件的代码 main.xml

<EditText
            android:id="@+id/etname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="text"
            android:imeOptions="actionDone" >

<Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:onClick="searchArray"
            android:text="@string/search" />

搜索数组

public void searchArray(View view){
        Resources res = getResources();
        TypedArray ta = res.obtainTypedArray(R.array.list);
        int n = ta.length();
        String[][] array = new String[n][];
        for (int i = 0; i < n; ++i) {
            int id = ta.getResourceId(i, 0);
            if (id > 0) {
                array[i] = res.getStringArray(id);
            } else {
                // something wrong with the XML
            }
        }
        ta.recycle(); 

        TextView name = (TextView) findViewById(R.id.etname);
        String stringname = name.getText().toString();

        if (stringname != null){
            for (int cont = 0; cont < n; ++cont) {
                if (!(array[cont][1].equals(stringname))){
                    Toast toast2 = Toast.makeText(getApplicationContext(), array[cont][1], Toast.LENGTH_SHORT);
                    toast2.show();
                } else {
                    Toast toast = Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        }

    }

【问题讨论】:

  • 下面 Szymon 的良好观察。我还注意到,尽管您在问题中说了什么,但您正在与 TextView 进行比较,而不是 EditText。
  • 不是错误,我改了还是不行

标签: android arraylist compare android-edittext


【解决方案1】:

您似乎是从索引 1 进行比较,而不是 0。更改代码

 for (int cont = 0; cont < n; ++cont)

for (int cont = 0; cont < n; cont++)

【讨论】:

  • 我认为错误在 for,但不是因为它应该显示 toast“错误”,但什么也没发生
  • n的值是多少?
  • 是的,我知道,但是调试时的值是多少。
【解决方案2】:

这样试试,

public void searchArray(View view){
        Resources res = getResources();
        TypedArray ta = res.obtainTypedArray(R.array.list);
        int n = ta.length();
        String[][] array = new String[n][];
        for (int i = 0; i < n; i++) {
            int id = ta.getResourceId(i, 0);
            if (id > 0) {
                array[i] = res.getStringArray(id);
                System.out.println(res.getStringArray(id));
            } else {
                // something wrong with the XML
            }
        }
        ta.recycle(); 

        EditText name = (EditText) findViewById(R.id.etname);// I changed TextView to EditText
        String stringname = name.getText().toString();

        if (stringname != null){
            for (int cont = 0; cont < n; ++cont) {
                System.out.println(array[cont][1]+" "+stringname);
                if ((array[cont][1].equals(stringname))){//Edit Here, If it matches, Show Toast
                    Toast toast2 = Toast.makeText(getApplicationContext(), array[cont][1], Toast.LENGTH_SHORT);
                    toast2.show();
                } else {
                    Toast toast = Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        }

    }

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    相关资源
    最近更新 更多