不使用 setOnClickListener 朗读 TextView 内容(自动文本转语音)
TextView content read aloud without setOnClickListener (Automatic 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) {
}
});
我没有测试过这个code.Let我知道它是否行得通:
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) {
}
});
我正在使用文本转语音应用程序的源代码。我希望自动朗读 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) {
}
});
我没有测试过这个code.Let我知道它是否行得通:
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) {
}
});