我想在文本框中显示的当前时间中添加从时间选择器中选择的小时或分钟的时间..我们如何更新时间和日期也会根据时间进行更改。 告诉我如何从时间选择器添加时间并添加当前时间和日期自动更改..
以下是TimePicker的代码...
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener (){
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
pHour = hourOfDay;
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(" "));
// tv.setText(mydate);
}
/** 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 */
tv=(TextView) findViewById(R.id.textView1);
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);
//mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
/** Display the current time in the TextView */
updateDisplay();
}
private String TimePickerDialog(MainActivity mainActivity,
OnTimeSetListener mTimeSetListener2, int pHour2) {
// TODO Auto-generated method stub
return null;
}
@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();*/
times = pHour*60 + pMinute;
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,true);
}
return null;
}
}