在android的时间选择器中选择时间后启动倒计时
我想建立一个应用程序,从时间选择器中选择时间,然后选择文本视图中的时间和倒计时开始,并具有小时、分钟和秒值。。。 代码是:-在android的时间选择器中选择时间后启动倒计时,android,countdowntimer,Android,Countdowntimer,我想建立一个应用程序,从时间选择器中选择时间,然后选择文本视图中的时间和倒计时开始,并具有小时、分钟和秒值。。。 代码是:- public class MainActivity extends Activity implements OnClickListener{ /** Private members of the class */ private TextView displayTime; private Button pickTime; pri
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',所以我应该如何解决它您不需要从displayTime获取时间。你需要从pHour和pMinute fields那里得到时间。 更改此项:
times = Integer.parseInt(displayTime.getText().toString());
为此:
次数=pHour*60+pmnutes
这不是寻找代码的网站。这是关于找到问题解决方案的资源。你有什么问题?你尝试了什么?如果我在文本中输入234这样的值,我的代码会更改00:00:00,这种格式和倒计时很好…但是如果我从时间选择器中选择时间,我有02:54这样的值,我不知道如何转换和启动倒计时我们的问题是如何将mm:ss格式的时间转换为秒?如果是这样的话,读一下你们最初的问题并重写它。添加与问题相关的代码。我已添加代码。@user3278684将此答案标记为解决方案。让其他用户知道,这个问题已经解决了。你能告诉我如何在这个代码中从对话框中删除am/pm并获得24小时的格式在询问之前总是阅读文档吗,android.app.TimePickerDialog.OnTimeSetListener,int,int,boolean)Constructor中的最后一个参数我想从时间选择器中添加soe时间,并根据运行时的时间自动更改日期,当我选择时间时,我会得到不正确的值,并且我的日期也不会更改…请帮助我解决问题。@Demand也有同样的问题