无需setOnClickListener即可大声朗读TextView内容(自动文本语音转换)

时间:2019-02-17 19:12:47

标签: android listener voice

我正在使用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) {
        }
});

1 个答案:

答案 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) {

    }
});