我正在编写一个应用程序,我需要从开始时间选择器获取用户设置时间,以及从结束时间选择器获取用户设置时间。然后将开始时间和结束时间与当前时间进行比较。如果当前时间等于开始时间,则手机静音。如果结束时间等于当前时间,则取消静音电话。任何帮助,尤其是代码示例,将不胜感激。提前谢谢。
public TimePickerDialog.OnTimeSetListener sTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
sHour = hourOfDay;
sMinute = minute;
updateDisplay();
displayToast();
}
};
public TimePickerDialog.OnTimeSetListener eTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
eHour = hourOfDay;
eMinute = minute;
updateDisplayE();
displayToastE();
}
};
public void updateDisplay() {
startTime.setText(new StringBuilder().append(pad(sHour)).append(":")
.append(pad(sMinute)));
final int compareStartHour = sHour;
Log.e("beginning", "Value " + sHour);
}
public void updateDisplayE()
{
endTime.setText(new StringBuilder().append(pad(eHour)).append(":")
.append(pad(eMinute)));
}
private void displayToast() {
Toast.makeText(
this,
new StringBuilder().append("Time choosen is ").append(
startTime.getText()), Toast.LENGTH_SHORT).show();
}
private void displayToastE() {
Toast.makeText(
this,
new StringBuilder().append("Time choosen is ").append(
endTime.getText()), Toast.LENGTH_SHORT).show();
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preset_edit);
save = (Button) findViewById(R.id.btnSave);
TextView eventTxt = (TextView) findViewById(R.id.txtViewEvent);
startTime = (Button) findViewById(R.id.btnStartTime);
endTime = (Button) findViewById(R.id.btnEndTime);
startTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
endTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID2);
}
});