首页 > 解决方案 > Android RECOGNIZE_SPEECH_ACTIVITY 对于问题(对于?)

问题描述

完成此方法后,我试图将字符串 (strSpeech2Text) 用字符制动,并将它们分别分配到 T1 T2 等文本视图中。

如果我不尝试重用 foreach,那么我得到的是整个单词被分配给每个文本视图。所以分配工作..

那有什么问题?

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case RECOGNIZE_SPEECH_ACTIVITY:
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> speech = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                String strSpeech2Text = speech.get(0);
                grabar.setText(strSpeech2Text);


                TextView T1;
                T1 = (TextView) findViewById(R.id.TIRADA0);
                TextView T2;
                T2 = (TextView) findViewById(R.id.TIRADA1);
                TextView T3;
                T3 = (TextView) findViewById(R.id.TIRADA2);
                TextView T4;
                T4 = (TextView) findViewById(R.id.TIRADA3);



                //String v;
                String a = strSpeech2Text;
                int n = 0;
                for (TextView i : Arrays.asList(T1, T2, T3, T4)) {
                    Character v = a.charAt(n);
                    i.setText(Character.toString(v));
                    n++;
                }


            }
            break;
        default:
            break;
    }
}

标签: javaandroid

解决方案


试试这个。我没有测试它,但它对我来说看起来不错:

ArrayList<TextView> textViews = Arrays.asList(T1, T2, T3, T4);    

for (int i=0; i<textViews.size() && strSpeech2Text.length>i; i++) {
    textViews.get(i).setText(strSpeech2Text.substring(i,i+1));
}

推荐阅读