我有一个问题,通过长按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;
}
}
答案 0 :(得分:0)
要识别EditText
中的输入并将输入转换为语音,您可以使用Android提供的TextToSpeech
类。例如:
textToSpeech = new TextToSpeech(MainActivity.this, MainActivity.this);
editText = (EditText) findViewById(R.id.editText);
imageViewClick = (ImageView) findViewById(R.id.imageViewClick);
imageViewClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextToSpeechFunction() ;
}});
}
public void TextToSpeechFunction()
{
String text = editText.getText().toString();
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
Toast.makeText(MainActivity.this , text, Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
textToSpeech.shutdown();
super.onDestroy();
}
@Override
public void onInit(int TexttoSpeechCurrentStatus) {
if (TexttoSpeechCurrentStatus == TextToSpeech.SUCCESS) {
textToSpeech.setLanguage(Locale.US);
imageViewClick.setEnabled(true);
TextToSpeechFunction();
}
}