如何将文本设置为TextViews数组。
这就是我所做的:
public void onClickButtons(View view) {
if(view==mBtnGuess) {
int i;
String word = "someword";
String getInput = mEtxtUserInput.getText().toString();
char[] wordChars = word.toCharArray();
if(getInput.length()>0) {
if(getInput.length()==1) {
List<TextView> txtCharArr= new ArrayList<TextView>();
txtCharArr.add(mChar1);
txtCharArr.add(mChar2);
txtCharArr.add(mChar3);
txtCharArr.add(mChar4);
txtCharArr.add(mChar5);
txtCharArr.add(mChar6);
txtCharArr.add(mChar7);
txtCharArr.add(mChar8);
txtCharArr.add(mChar9);
txtCharArr.add(mChar10);
txtCharArr.add(mChar11);
txtCharArr.add(mChar12);
StringBuilder sb = new StringBuilder();
for(i=0;i<wordChars.length;i++) {
if(wordChars[i]==(word.charAt(i))) {
txtCharArr.get(i).setText(word.charAt(i));
sb.append(word.charAt(i));
}
}
}
}
}
}
stringbuilder只是为了保存所有相等的字符。上面的代码给出了一个错误:
03-14 07:44:27.387: E/AndroidRuntime(1893): FATAL EXCEPTION: main
03-14 07:44:27.387: E/AndroidRuntime(1893): java.lang.IllegalStateException: Could not execute method of the activity
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.view.View$1.onClick(View.java:3591)
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.view.View.performClick(View.java:4084)
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.view.View$PerformClick.run(View.java:16966)
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.os.Handler.handleCallback(Handler.java:615)
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.os.Looper.loop(Looper.java:137)
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-14 07:44:27.387: E/AndroidRuntime(1893): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 07:44:27.387: E/AndroidRuntime(1893): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 07:44:27.387: E/AndroidRuntime(1893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-14 07:44:27.387: E/AndroidRuntime(1893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 07:44:27.387: E/AndroidRuntime(1893): at dalvik.system.NativeStart.main(Native Method)
03-14 07:44:27.387: E/AndroidRuntime(1893): Caused by: java.lang.reflect.InvocationTargetException
03-14 07:44:27.387: E/AndroidRuntime(1893): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 07:44:27.387: E/AndroidRuntime(1893): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.view.View$1.onClick(View.java:3586)
03-14 07:44:27.387: E/AndroidRuntime(1893): ... 11 more
03-14 07:44:27.387: E/AndroidRuntime(1893): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x76
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.content.res.Resources.getText(Resources.java:229)
03-14 07:44:27.387: E/AndroidRuntime(1893): at android.widget.TextView.setText(TextView.java:3620)
03-14 07:44:27.387: E/AndroidRuntime(1893): at me.mikey.my.games.galgjex.Galgje.onClickButtons(Galgje.java:232)
第232行是:
txtCharArr.get(i).setText(word.charAt(i));
我也尝试过:
public void onClickButtons(View view) {
if(view==mBtnGuess) {
int i;
String word = "someword";
String getInput = mEtxtUserInput.getText().toString();
if(getInput.length()>0) {
if(getInput.length()==1) {
List<TextView> txtCharArr= new ArrayList<TextView>();
txtCharArr.add(mChar1);
txtCharArr.add(mChar2);
txtCharArr.add(mChar3);
txtCharArr.add(mChar4);
txtCharArr.add(mChar5);
txtCharArr.add(mChar6);
txtCharArr.add(mChar7);
txtCharArr.add(mChar8);
txtCharArr.add(mChar9);
txtCharArr.add(mChar10);
txtCharArr.add(mChar11);
txtCharArr.add(mChar12);
StringBuilder sb = new StringBuilder();
for(i=0;i<getInput.length();i++) {
if(getInput.equals(word.charAt(i))) {
txtCharArr.get(i).setText(word.charAt(i));
sb.append(word.charAt(i));
}
}
}
}
}
}
但这并没有显示任何内容。 StringBuilder为空,当我输入一个应匹配的字符时,textViews中没有显示任何字符。
答案 0 :(得分:1)
由于没有TextView.setText(char x)
,您的char被强制转换为int,并调用TextView.setText(int x)
,它会搜索ID为x
的资源。
您应该将字符串传递给setText()
方法。你可以尝试
txtCharArr.get(i).setText(Character.toString(word.charAt(i)));
或
txtCharArr.get(i).setText(word.subString(i, i+1)));