我用了一个tts来读取我的文本视图,但是一旦我打开活动,它就开始阅读,但我不希望这样,我想按下按钮然后它开始阅读,这是代码:
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private Button buttonSpeak;
private Textview tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
buttonSpeak = (Button) findViewById(R.id.button1);
tv= (Textview) findViewById(R.id.tv1);
buttonSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
} });}
@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy(); }
@Override
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 {
buttonSpeak.setEnabled(true);
speakOut(); } } else {
Log.e("TTS", "Initilization Failed!");
}}
private void speakOut() {
String text = Textview.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); }}
Call speakOut((; from onCreate((
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
buttonSpeak = (Button) findViewById(R.id.button1);
tv= (Textview) findViewById(R.id.tv1);
speakOut();
}
不要从 onInit(( 内部调用 speakout((。