我正在使用Text-To-Speech应用程序的源代码。我希望自动朗读TextView的内容(即不使用 setOnClickListener )。有人知道该如何处理吗?提前谢谢!
编辑:这是最终结果,并且有效:
mVoiceOutputTv.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = charSequence.toString();
if (text.contains(text)) {
if (timer != null) {
timer.cancel();
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toSpeak = mVoiceOutputTv.getText().toString();
textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
}, 1000);
}
}
@Override
public void afterTextChanged (Editable editable) {
}
});
答案 0 :(得分:0)
我尚未测试此代码。请告诉我它是否无效:
Timer timer;
textView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(timer != null) {
timer.cancel();
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//Do your reading out here
}
},1000);//1000 milliseconds change it to whatever interval you want.
}
@Override
public void afterTextChanged(Editable editable) {
}
});