java - 我有文字转语音功能,显示按钮文本,但所有按钮都显示第一个按钮的文本

标签 java android text-to-speech

我刚刚完成编码,让文本转语音按钮读取用户按下的按钮上的文本。出于某种原因,每个按钮都会显示第一个按钮上的文本,而不是它们自己的文本。显然这是一个问题,因为您不希望每个按钮都说同样的事情。它在我的 LogCat 中没有记录任何错误,因此它工作正常,只是不是我想要的那样。我没有java经验来查找问题的根源。

public class menu extends Activity implements TextToSpeech.OnInitListener,
        OnClickListener {

    TextToSpeech mTts;
    Button speakButton, infoButton, voiceButton;

    // TTS object
    public TextToSpeech myTTS;
    // status check code
    public int MY_DATA_CHECK_CODE = 0;

    @Override
    protected void onCreate(Bundle aboutmenu) {
        super.onCreate(aboutmenu);
        setContentView(R.layout.mainx);

        SpeakingAndroid speak = new SpeakingAndroid();

        VoiceRecognition voiceinput = new VoiceRecognition();

        // get a reference to the button element listed in the XML layout
        speakButton = (Button) findViewById(R.id.btn_speak);
        infoButton = (Button) findViewById(R.id.aboutbutton);
        voiceButton = (Button) findViewById(R.id.voicebutton);

        // listen for clicks
        infoButton.setOnClickListener(this);
        speakButton.setOnClickListener(this);


        // check for TTS data
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);



        voiceButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

            }
        });

    }
    public void onClick1(View about) {

        // get the text entered
        infoButton = (Button) findViewById(R.id.aboutbutton);
        String words = infoButton.getText().toString();
        speakWords(words);
        Intent infoIntent = new Intent("android.intent.action.INFOSCREEN");
        startActivity(infoIntent);

    }
    // respond to button clicks
    public void onClick(View v) {

        // get the text entered
        speakButton = (Button) findViewById(R.id.btn_speak);
        String words = speakButton.getText().toString();
        speakWords(words);
        Intent voiceIntent = new Intent("android.intent.action.RECOGNITIONMENU");
        startActivity(voiceIntent);

    }



    // speak the user text
    public void speakWords(String speech) {

        // speak straight away
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
    }

    // act on result of TTS data check
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // the user has the necessary data - create the TTS
                myTTS = new TextToSpeech(this, this);
            } else {
                // no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent
                        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }

    // setup TTS
    public void onInit(int initStatus) {

        // check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        } else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }

}

最佳答案

在您的 Activity 中声明文本转语音属性

private TextToSpeech    mTTS;

在 Activity 中实例化文本转语音对象

mTTS=new TextToSpeech(this,this);

获取对 XML 布局中列出的按钮的引用

speakButton = (Button) findViewById(R.id.btn_speak);
infoButton = (Button) findViewById(R.id.btn_about);
voiceButton = (Button) findViewById(R.id.btn_voice);

监听 Button 的 ClickEvents,您的 Activity 必须实现 View.OnClickListener 接口(interface)

infoButton.setOnClickListener(this);
speakButton.setOnClickListener(this);
voiceButton.setOnClickListener(this);

在重写 onClick() 方法中处理点击事件:

@Override
public void onClick(View v) {
    switch(v.getId())
    {
        case R.id.btn_speak:
            mTTS.speak(speakButton.getText().toString(), TextToSpeech.QUEUE_ADD, null);
        break;
        case R.id.btn_about:
            mTTS.speak(infoButton.getText().toString(), TextToSpeech.QUEUE_ADD, null);
        break;
        case R.id.btn_voice:
            mTTS.speak(voiceButton.getText().toString(), TextToSpeech.QUEUE_ADD, null);
        break;
    }

}

关于java - 我有文字转语音功能,显示按钮文本,但所有按钮都显示第一个按钮的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11653509/

相关文章:

java - 从 Android 网站获取信息

android - 如何让我的 TTS 不与谷歌地图语音重叠(用于行车方向)?

java - 在 Java 中序列化然后在 C++ 中反序列化?

android - 如何在Timer中使用OpenGL

java - Spring data jpa @Onetomany问题

Java 字节数组到有符号 Int

android - 如何在 TextTOSpeech 中设置印度英语?

c++ - QTextToSpeech 设置默认 QLocale

java - @EnableBatchProcessing 破坏了 MockMvc 测试

java - 为什么我运行它时显示 Hello World?