将数据从一个活动传递到另一活动的Textview

时间:2018-07-08 12:38:04

标签: android-intent android-activity textview

我的项目
您可以对着麦克风讲话,然后看到输出。

我的实际问题:
现在执行代码时,我可以发言,查看输出并跳到下一个活动。

我希望的情况:
在我的第二个活动中,有一个TextView。我可以将数据传递到另一个活动的TextView吗?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TextView voiceInput;
    private ImageView 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 = (ImageView) 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, "Sprechen Sie was ein");
        //tent.putStringArrayListExtra("result",resultat);
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {

        }
    }
    @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);
                    //Ausgabe in andere Activity
                    voiceInput.setText(result.get(0));
                    Intent intent=new Intent(this,Zweites.class);
                    //intent.putStringArrayListExtra("resultat",result);
                    intent.putExtra("resultat", result.get(0));
                    //ActivityZweites wird gestartet
                    startActivity(intent);
                }
                break;
            }

        } }}


如果有任何链接可以帮助我解决问题,请在此处添加它们。我可以自己去搜索。
我添加了一些图片,以便您可以想象我的想法。
Here it is right now - I click on the mic - The output will be opened and then I directly jump into the second activity

But what I want is click on mic - and then jump into the second activity where I see my output in a TextView like in this picutre

0 个答案:

没有答案