2014-09-11 61 views
2

如何將此TOAST消息轉換爲Android語音?將Toast消息轉換爲文本

Toast.makeText(MainActivity.this, "I am enter code here" +positive[+ position]+ " always", Toast.LENGTH_SHORT).show(); 
+0

您是否試圖讓Android發言? – Shane 2014-09-11 02:42:41

+0

是的,永遠是應該轉換爲語音的Toast消息彈出窗口。 – user3882389 2014-09-11 09:53:17

回答

0
TextView wordView = (TextView)view; String wordChosen = (String) wordView.getText(); Toast.makeText(MainActivity.this, "I am " + positive[+position] + " always", Toast.LENGTH_SHORT).show(); //tts.speak("I am" + blank +position+" always", TextToSpeech.QUEUE_FLUSH, null); tts.speak("You chose, you are "+wordChosen+" today and always", TextToSpeech.QUEUE_FLUSH, null); 
1

先導入包

​​

然後初始化

private TextToSpeech tts; 
    tts = new TextToSpeech(this, this); 

最後作出這樣的函數

private void speakOut() { 

    String text = txtText.getText().toString(); 

    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
} 

Woops。我忘了,你還需要定義一個OnInit的功能

public void onInit(int status) { 

    if (status == TextToSpeech.SUCCESS) { 

     int result = tts.setLanguage(Locale.US); 

     if (result == TextToSpeech.LANG_MISSING_DATA 
       || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
      Log.e("TTS", "This Language is not supported"); 
     } else { 
      btnSpeak.setEnabled(true); 
      speakOut(); 
     } 

    } else { 
     Log.e("TTS", "Initilization Failed!"); 
    } 

} 

在這個例子中我用了一個onClickListener調用這個函數,使用按鈕。將其修改爲當您爲消息吐司時如何調用此功能。

只要將字符串text作爲您的敬酒信息。在上面的例子中,txtText是一個editText。根據您的要求修改

+0

太棒了,謝謝! – user3882389 2014-09-11 09:50:37

+0

TextView wordView =(TextView)view; String wordChosen =(String)wordView.getText(); Toast.makeText(MainActivity.this,「I am」+ positive [+ position] +「always」,Toast.LENGTH_SHORT).show(); //tts.speak("I am「+ blank + position +」always「,TextToSpeech.QUEUE_FLUSH,null); (「你選擇了,你是」+ wordChosen +「,今天總是」,TextToSpeech.QUEUE_FLUSH,null); – user3882389 2014-11-20 03:33:47