gpt4 book ai didi

android - 单击 EditText 的 TimePicker 对话框

转载 作者:IT老高 更新时间:2023-10-28 13:06:45 26 4
gpt4 key购买 nike

我已经有了一个 DatePicker,当用户点击 EditText 字段时会弹出它

eReminderDate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//To show current date in the datepicker
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

DatePickerDialog mDatePicker;
mDatePicker = new DatePickerDialog(AddReminder.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
selectedmonth = selectedmonth + 1;
eReminderDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select Date");
mDatePicker.show();
}
});

我尝试以类似的方式执行 TimePicker,但无法使其正常工作。这是我让它工作的尝试

 eReminderTime.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
eReminderTime.setText( ""selectedHour + ":" + selectedMinute);
}
}, hour, minute);
mTimePicker.setTitle("Select Time");
mTimePicker.show();

}
});

难道不能像我为 DatePicker 那样做吗?

我什至尝试过在使用此代码单击 EditText 字段后弹出 TimePicker。

      eReminderTime.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(TIME_DIALOG_ID);
}
});

由于某种原因,当我将其输入 Android Studio 时,“showDialog”被计分了。

谁能告诉我哪里出错了?还是我只需要使用自定义 DialogFragment?

最佳答案

eReminderTime.setText( "" + selectedHour + ":" + selectedMinute);

您在 "" 和选定的小时之间缺少一个 +setText 方法仅采用单个字符串,因此您需要连接所有部分(可能不需要第一个引号)。

eReminderTime.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
eReminderTime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();

}
});

这应该可以解决您的第二个错误,因为您没有提供最后一个参数。 TimePickerDialog Constructors

关于android - 单击 EditText 的 TimePicker 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17901946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com