我有一个问题,通过长按imageView“imageViewClick.setOnLongClickListener”识别语音并回答问题。如何使正常按“imageViewClick.setOnClickListener”识别editText的文本并回答问题?
我想知道如何从editText读取文本并显示答案
谢谢
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
imageViewClick = (ImageView) findViewById(R.id.imageViewClick);
imageViewClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
imageViewClick.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "es-ES");
voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "es-MX");
voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "pr-PR");
startActivityForResult(voice, VOICE_RECOGNIZER);
return false;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == VOICE_RECOGNIZER){
ArrayList<String> recognized = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String listened = recognized.get(0);
editTextListening.setText(listened);
prepareAnswer(listened);
}
}
private void prepareAnswer(String listened) {
String normalize = Normalizer.normalize(listened, Normalizer.Form.NFD);
String withouttilde = normalize.replaceAll("[^\\p{ASCII}]", "");
int result;
String answer = arrayListAnswer.get(0).getAnswer();
for (int i = 0; i < arrayListAnswer.size(); i++) {
result = withouttilde.toLowerCase().indexOf(arrayListAnswer.get(i).getQuestion());
if(result != -1){
answer = arrayListAnswer.get(i).getAnswer();
}
}
answerTo(answer);
}
private void answerTo(String simpleAnswer) {
textViewAnswer.setText(simpleAnswer);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeechRead.speak(simpleAnswer, TextToSpeech.QUEUE_FLUSH, null, null);
}else {
textToSpeechRead.speak(simpleAnswer, TextToSpeech.QUEUE_FLUSH, null);
}
}
public void initialize(){
editTextListening = (EditText) findViewById(R.id.editTextQuestion);
textViewAnswer = (TextView) findViewById(R.id.textViewAnswer);
arrayListAnswer = provideData();
textToSpeechRead = new TextToSpeech(this, this);
}
@Override
public void onInit(int status) {
}
public ArrayList<AnswersActivity> provideData(){
ArrayList<AnswersActivity> answers = new ArrayList<>();
answers.add(new AnswersActivity("defecto", "No estoy programado para hablar de eso"));
answers.add(new AnswersActivity("puff", "Puff"));
answers.add(new AnswersActivity("chiste", "¿Sabes que mi hermano anda en bicicleta desde los 4 años? Mmm, ya debe estar lejos"));
answers.add(new AnswersActivity("adios", "que descanses"));
answers.add(new AnswersActivity("estas", "esperando serte de ayuda"));
answers.add(new AnswersActivity("dj", "YEY BALBIN"));
return answers;
}
}
1条答案
按热度按时间bnl4lu3b1#
要识别
EditText
中的输入并将输入转换为语音,您可以使用Android提供的TextToSpeech
类。例如: