猿问

第二个活动中的输出不起作用

我在Android Studio上进行语音识别。


MainActivity.java:


public class MainActivity extends AppCompatActivity {


    private TextView voiceInput;

    private TextView speakButton;

    private final int REQ_CODE_SPEECH_INPUT = 100;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        voiceInput = (TextView) findViewById(R.id.voiceInput);

        speakButton = (TextView) findViewById(R.id.btnSpeak);


        speakButton.setOnClickListener(new View.OnClickListener() {


            @Override

            public void onClick(View v) {

                askSpeechInput();

            }

        });

    }


    // Showing google speech input dialog


    private void askSpeechInput() {

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,

                "Hi speak something");

        try {

            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);

        } catch (ActivityNotFoundException a) {


        }

    }


    // Receiving speech input


    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);


        switch (requestCode) {

            case REQ_CODE_SPEECH_INPUT: {

                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                    voiceInput.setText(result.get(0));

                }

                break;

            }

        }

    }

}

我的问题
现在,我创建了第二个活动,并希望在那里接收输出,因此我在上面复制了这些代码块并将其粘贴。它没有用。
如何让口头输入的输出出现在第二个活动中?
如果您需要更多信息,请让我知道,对于愚蠢的问题,我们感到抱歉,我认为解决方案非常简单,但我无法对其进行罚款!

DIEA
浏览 157回答 2
2回答

qq_遁去的一_1

You can pass an ArrayList<E> to Your SecondActivity.例子:ArrayList<String> yourList = new ArrayList<String>();intent.putExtra("yourlist", yourList);在另一个活动中:ArrayList<String> yourList = (ArrayList<String>)&nbsp;getIntent().getSerializableExtra("yourlist");

慕标琳琳

尝试将结果数据发送到第二个活动中,如下面的代码。&nbsp;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; switch (requestCode) {&nbsp; &nbsp; &nbsp; &nbsp; case REQ_CODE_SPEECH_INPUT: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (resultCode == RESULT_OK && null != data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> result = data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; voiceInput.setText(result.get(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent=new Intent(this,SecondActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putStringArrayListExtra("result",result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在第二行的onCreate方法中进行第二次活动以获取结果。result_list = getIntent().getStringArrayListExtra("result");之后,result_list数据将显示到textview和其他视图中。确保两个活动都定义到android清单文件中。
随时随地看视频慕课网APP

相关分类

Java
我要回答