所以我在大约一年前设计了一个Android应用程序,现在在Android Studio中处理我学院的一些事情。但是,我从来没有完全完成"它。它缺少的一件事是当我点击一个按钮它启动一个计时器并输出一个字符串。但是,如果我在该计时器启动之前按下另一个按钮,它将同时说两个按钮。在网上看,我还没找到我想要的东西,所以我想我会把它打开到Stackoverflow社区,也许你可以引导我朝着正确的方向前进。现在是代码。
package com.example.james.texttospeech;
import android.os.Bundle;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.content.Intent;
import java.util.Locale;
import android.widget.Toast;
import android.os.CountDownTimer;
public class MainActivity extends Activity implements OnClickListener, OnInitListener {
//TTS object
private TextToSpeech myTTS;
//status check code
private int MY_DATA_CHECK_CODE = 0;
//create the Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get a reference to the button element listed in the XML layout
Button speakButton = (Button)findViewById(R.id.speak);
//listen for clicks
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);
}
//respond to button clicks
public void onClick(View v) {
//get the text entered
EditText enteredText = (EditText)findViewById(R.id.enter);
String words = enteredText.getText().toString();
speakWords(words);
}
public void BRC (View view) {
new CountDownTimer(65000, 6000) {
public void onTick(long millisUntilFinished) {
String words1 = ("BRC will form up in" + millisUntilFinished / 6000 + " minutes");
speakWords(words1);
}
public void onFinish() {
String words2 = ("BRC will form up right away");
speakWords(words2);
}
}.start();
}
public void SRC (View view1) {
new CountDownTimer(65000, 6000) {
public void onTick(long millisUntilFinished) {
String words3 = ("SRC will form up in" + millisUntilFinished / 6000 +" minutes");
speakWords(words3);
}
public void onFinish() {
String words4=("SRC will form up right away");
speakWords(words4);
}
}.start();
}
public void Taps (View view2) {
new CountDownTimer(65000, 6000) {
public void onTick(long millisUntilFinished) {
String words5 = ("Taps will sound in" + millisUntilFinished / 6000 +" minutes");
speakWords(words5);
}
public void onFinish() {
String words6=("Attention inside and outside of barracks the status in barracks is now Taps C C Q at the beginning of this turnout there was a status check");
speakWords(words6);
}
}.start();
}
public void Penalty_Tours (View view3) {
new CountDownTimer(65000, 6000) {
public void onTick(long millisUntilFinished) {
String words7 = ("P Tees will form up in" + millisUntilFinished / 6000 +" minutes");
speakWords(words7);
}
public void onFinish() {
String words8=("P Tees will form up right away");
speakWords(words8);
}
}.start();
}
public void Colors (View view4) {
String words9 = ("Colors Colors Colors");
speakWords(words9);
}
public void PTT (View view5) {
new CountDownTimer(65000, 6000) {
public void onTick(long millisUntilFinished) {
String words10 = ("P T T will form up in" + millisUntilFinished / 6000 +" minutes");
speakWords(words10);
}
public void onFinish() {
String words11=("P T T will form up right away");
speakWords(words11);
}
}.start();
}
//speak the user text
private 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();
}
}
}
非常感谢任何帮助。
public void BRC(View view) {
int totaltime = 6500;
CountDownTimer mTimer = new CountDownTimer(totaltime, 6000) {
public void onTick(long millisUntilFinished) {
String words1 = ("BRC will form up in" + millisUntilFinished / 6000 + " minutes");
speakWords(words1);
}
public void onFinish() {
String words2 = ("BRC will form up right away");
speakWords(words2);
}
}.start();
void SRC() {
mTimer.cancel();
}
}
答案 0 :(得分:0)
在MainActivity函数中,声明一个变量来定义每个计数器的状态。在所有计数器的onTick()中,使其检查变量
希望得到这个帮助。
public class MainActivity extends Activity implements OnClickListener, OnInitListener {
//TTS object
private Boolean isBRCRunning = false;
private Boolean isPTTRunning = false;
private static CountDownTimer myBRC;
private static CountDownTimer myPTT;
//
//respond to button clicks
public void onClick(View v) {
// .....
// If BRC button
if(BRC button){
BRC();
isBRCRunning = true;
} else if (PTT button){
BRC();
isBRCRunning = true;
}
}
public void BRC () {
myBRC = new CountDownTimer(65000, 6000) {
public void onTick(long millisUntilFinished) {
String words1 = ("BRC will form up in" + millisUntilFinished / 6000 + " minutes");
MainActivity.logForDebug(MainActivity.TAG, words1);
// Control other timer
if(isPTTRunning){
myPTT.cancel();
isPTTRunning = false;
}
}
public void onFinish() {
String words2 = ("BRC will form up right away");
MainActivity.logForDebug(MainActivity.TAG, words2);
}
}.start();
}
public void PTT () {
myPTT = new CountDownTimer(65000, 6000) {
public void onTick(long millisUntilFinished) {
String words1 = ("BRC will form up in" + millisUntilFinished / 6000 + " minutes");
MainActivity.logForDebug(MainActivity.TAG, words1);
// Control other timers
if(isBRCRunning){
myBRC.cancel();
isBRCRunning = false;
}
}
public void onFinish() {
String words2 = ("BRC will form up right away");
MainActivity.logForDebug(MainActivity.TAG, words2);
}
}.start();
}
}