gpt4 book ai didi

java - 如何让ActivityResult输出到2个不同的textview?

转载 作者:行者123 更新时间:2023-12-02 01:39:27 25 4
gpt4 key购买 nike

我想将语音转换为文本应用程序,但在不同的 TextView 上输出。我尝试在互联网上搜索解决方案,但大多数答案仅输出到 1 个 TextView 。我想让它看起来像这样:

这是我的想法的形象:/image/orTec.png

这是为了学习目的。我希望有人能帮助我

    TextView textview1, textview2;
Button btnVoice1, btnVoice2;

textview1= findViewById(R.id.textview1);
textview2= findViewById(R.id.textview2);

btnVoice1 = findViewById(R.id.btnVoice1);
btnVoice2 = findViewById(R.id.btnVoice2);

btnVoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startVoiceInput();
}
});

btnVoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startVoiceInput();
}
});

//函数

    private void startVoiceInput(){
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, "Say Something!");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}

@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);
textview1.setText(result.get(0));
// how to make textview2 output when btnVoice2 is click
}
break;
}

}
}

最佳答案

对于单个 Activity 的多个 onActivityResult,请像这样更改代码:

    int REQ_CODE_SPEECH_INPUT=100;
int REQ_CODE_SPEECH_INPUT_SECOND=101;
TextView textview1, textview2;

Button btnVoice1, btnVoice2;

textview1= findViewById(R.id.textview1);
textview2= findViewById(R.id.textview2);

btnVoice1 = findViewById(R.id.btnVoice1);
btnVoice2 = findViewById(R.id.btnVoice2);

btnVoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startVoiceInput(REQ_CODE_SPEECH_INPUT);
}
});

btnVoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startVoiceInput(REQ_CODE_SPEECH_INPUT_SECOND);
}
});


private void startVoiceInput(int code){
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, "Say Something!");
try {
startActivityForResult(intent, code);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}


@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);
textview1.setText(result.get(0));

}
break;
}
case REQ_CODE_SPEECH_INPUT_SECOND:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textview2.setText(result.get(0));

}
break;
}


}
}

关于java - 如何让ActivityResult输出到2个不同的textview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57497497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com