此代码尝试捕获 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();
}
}
}
}