获取 timePicker 值而不显示 timePicker 对话框
Posted
技术标签:
【中文标题】获取 timePicker 值而不显示 timePicker 对话框【英文标题】:Get timePicker value without showing timePicker dialog 【发布时间】:2016-08-01 22:24:32 【问题描述】:如何在不显示timePicker
对话框的情况下获取选定的时间值?
下图是我的报警设计布局。单击按钮时,我希望它显示所选值而不是弹出 timePicker 对话框。
alarm.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showDialog(TIME_DIALOG_ID);
);
@Override
protected Dialog onCreateDialog(int id)
switch (id)
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
return null;
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener()
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes)
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour,minute);
;
// Used to convert 24hr format to 12hr format with AM/PM values
private void updateTime(int hours, int mins)
String timeSet = "";
if (hours > 12)
hours -= 12;
timeSet = "PM";
else if (hours == 0)
hours += 12;
timeSet = "AM";
else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (mins < 10)
minutes = "0" + mins;
else
minutes = String.valueOf(mins);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(':')
.append(minutes).append(" ").append(timeSet).toString();
Toast.makeText(getApplicationContext(),aTime,Toast.LENGTH_LONG).show();
我应该在哪里更改按钮单击?谢谢。
【问题讨论】:
【参考方案1】:内部按钮点击,删除
showDialog(TIME_DIALOG_ID);
并替换为
int hour = alarmTimePicker.getCurrentHour();
int min = alarmTimePicker.getCurrentMinute();
updateTime(hour,min);
alarmTimePicker 遵循 timePicker
名称,用于初始化 timePicker
。
例如:
TimePicker alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
删除下面的代码,因为不需要对话框。
@Override
protected Dialog onCreateDialog(int id)
switch (id)
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
return null;
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener()
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes)
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour,minute);
;
【讨论】:
以上是关于获取 timePicker 值而不显示 timePicker 对话框的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin.Forms Timepicker IsVisible 属性与自定义渲染器