本文介绍了无需setOnClickListener即可大声朗读TextView内容(自动文本语音转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Text-To-Speech应用程序的源代码.我希望自动朗读TextView的内容(即不使用 setOnClickListener ).有谁知道如何解决这个问题?提前谢谢!

I am using the source code of a Text-To-Speech app. I would like the content of a TextView to be read aloud automatically (i.e. without the use of setOnClickListener). Does anyone know how to approach this? Thanks in advance!

这是最终结果,并且有效:

This is the final result, and it works:

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) {
        }
});

推荐答案

我尚未测试此代码.请让我知道它是否不起作用:

I have not tested this code.Let me know if it doesn't work:

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

    }
});

这篇关于无需setOnClickListener即可大声朗读TextView内容(自动文本语音转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 04:06