TextView content read aloud without setOnClickList

2019-08-23 11:55发布

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!

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

1条回答
聊天终结者
2楼-- · 2019-08-23 12:21

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

    }
});
查看更多
登录 后发表回答