0

如何将文本设置为 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 中没有显示任何字符。

4

1 回答 1

1

因为没有TextView.setText(char x)char ,所以您的 char 被强制转换为 int 并被TextView.setText(int x)调用,它搜索具有 id 的资源x

您应该将 String 传递给该setText()方法。你可以试试

txtCharArr.get(i).setText(Character.toString(word.charAt(i)));

或者

txtCharArr.get(i).setText(word.subString(i, i+1)));

于 2013-03-14T08:41:26.223 回答