我想从用户输入的TimePickerDialog中获取hourOfDay和分钟,但是从onTimeSet方法中将值显示为零。
LoadTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar = Calendar.getInstance();
currentHour = calendar.get(Calendar.HOUR_OF_DAY);
currentMinute = calendar.get(Calendar.MINUTE);
timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
if (hourOfDay >= 12) {
amPm = "PM";
} else {
amPm = "AM";
}
LoadTime.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
}
}, currentHour, currentMinute, false);
Log.d("INT","val: " + hour);
timePickerDialog.show();
}
});
我想保存这两个变量并执行一些if-else条件。提前致谢。
最佳答案
您正在使用onTimeSet()
方法之外的日志。
LoadTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar = Calendar.getInstance();
currentHour = calendar.get(Calendar.HOUR_OF_DAY);
currentMinute = calendar.get(Calendar.MINUTE);
timePickerDialog = new TimePickerDialog(MainActivity.this, new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
if (hourOfDay >= 12) {
amPm = "PM";
} else {
amPm = "AM";
}
Log.d("INT","val: " + hourOfDay);
LoadTime.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
}
}, currentHour, currentMinute, false);
timePickerDialog.show();
}
});`
为了将值存储在
string
中,java字符串.valueOf()
方法将不同类型的值转换为字符串。例如:
String hourOfDayInString = String.valueOf(hourOfDay);
关于android - 如何保存timePickerDialog中的hourOfDay和分钟,以将其与其他变量进行比较?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54184416/