音声合成ボタンにユーザーが押したボタンのテキストを読み取らせるようにコーディングを完了しました。何らかの理由で、すべてのボタンが自分のテキストではなく最初のボタンのテキストを表示します。すべてのボタンが同じことを言うのは望ましくないので、明らかにこれは問題です。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();
}
}
}