我想構建應用程序,從時間選擇器中選擇時間,然後選擇文本視圖中的時間,倒數計時器開始計時並具有小時,分鐘,和秒數值... 的代碼是: -當我從時間選擇器中選擇時間時,開始倒數計時器android
public class MainActivity extends Activity implements OnClickListener{
/** Private members of the class */
private TextView displayTime;
private Button pickTime;
private Button buttonStartTime, buttonStopTime;
private TextView textViewShowTime; // will show the time
private CountDownTimer countDownTimer; // built in android class
private long totalTimeCountInMilliseconds;
private int pHour;
private int pMinute;
/** This integer will uniquely define the dialog to be used for displaying time picker.*/
static final int TIME_DIALOG_ID = 0;
/** Callback received when the user "picks" a time in the dialog */
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
pHour =pHour+ hourOfDay;
pMinute = pMinute+minute;
updateDisplay();
displayToast();
}
};
/** Updates the time in the TextView */
private void updateDisplay() {
displayTime.setText(
new StringBuilder()
.append(pad(pHour)).append(":")
.append(pad(pMinute)));
tv.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-")
.append(mMonth + 1).append("-")
.append(mYear).append(" "));
}
/** Displays a notification when the time is updated */
private void displayToast() {
Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(displayTime.getText()), Toast.LENGTH_SHORT).show();
}
/** Add padding to numbers less than ten */
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Capture our View elements */
displayTime = (TextView) findViewById(R.id.timeDisplay);
pickTime = (Button) findViewById(R.id.pickTime);
buttonStartTime = (Button) findViewById(R.id.btnStartTime);
buttonStopTime = (Button) findViewById(R.id.btnStopTime);
displayTime = (TextView) findViewById(R.id.timeDisplay);
buttonStartTime.setOnClickListener(this);
buttonStopTime.setOnClickListener(this);
/** Listener for click event of the button */
pickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
/** Get the current time */
final Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
pHour = cal.get(Calendar.HOUR_OF_DAY);
pMinute = cal.get(Calendar.MINUTE);
/** Display the current time in the TextView */
updateDisplay();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnStartTime) {
// textViewShowTime.setTextAppearance(getApplicationContext(),
// R.style.normalText);
setTimer();
buttonStopTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.GONE);
//edtTimerValue.setVisibility(View.GONE);
//edtTimerValue.setText("");
startTimer();
} else if (v.getId() == R.id.btnStopTime) {
countDownTimer.cancel();
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
}
}
private void setTimer() {
int times = 0;
if (!displayTime.getText().toString().equals("")) {
times = Integer.parseInt(displayTime.getText().toString());
//time=timer;
} else
Toast.makeText(MainActivity.this, "Please Enter Minutes...",
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds = 60 * times * 1000;
//timeBlinkInMilliseconds = 30 * 1000;
}
private void startTimer() {
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
@Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds/1000;
displayTime.setText(String.format("%02d:%02d:%02d", seconds/3600,
(seconds % 3600)/60, (seconds % 60)));
// format the textview to show the easily readable format
}
@Override
public void onFinish() {
// this function will be called when the timecount is finished
displayTime.setText("Time up!");
displayTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
//edtTimerValue.setVisibility(View.VISIBLE);
}
}.start();
}
/** Create a new dialog for time picker */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, pHour, pMinute, false);
}
return null;
}
}
它使無效INT錯誤:因爲在文本中的值是'43:56' ,所以我應該怎麼解決呢
這不是網站上找到的代碼。這是找到問題解決方案的資源。你有什麼問題?你嘗試了什麼? –
如果我輸入文字的值如234我的代碼更改00:00:00它這種格式和倒計時工作正常...但如果我選擇時間選擇器我有像02:54價值,我不dont knw如何轉換和啓動倒數計時器 – user3278684
您的問題是如何將格式mm:ss中的時間轉換爲秒?如果是這樣,讀你最初的問題,並重寫它。添加與您的問題相關的代碼。 –