例如,当我在18:00创建这个timePicker(如下图),该timePicker的TIMEVALUE将是06:00。在19:44-> 07:44 ...所以它不会从AM / PM切换到24小时模式后,移动到正确的当前小时。我怎样才能改变这种状况? 我的目标是,timePicker显示当前时间创建时。喜欢它,当我在AM / PM模式中使用它。
TimePicker timePicker =(TimePicker)convertView.findViewById(R.id.time_picker); timePicker.setIs24HourView(真正的);
Sry基因的不好的配方,希望你能理解我的问题。否则,只问。
更新:
公共类ChooseTimeViewDialog扩展AlertDialog { 保护上下文的背景下; 公共ChooseTimeViewDialog(最终上下文的背景下){ 超(上下文); this.context =背景; } 公共无效的onCreate(包savedInstanceState){ LayoutInflater充气= this.getLayoutInflater(); 查看convertView = inflater.inflate(R.layout.dialog_choose_time_view,NULL); 的setContentView(convertView); 的setTitle(R.string.title_dialog_choose_time_view); 最后TimePicker taskTimePicker =(TimePicker)convertView.findViewById(R.id.dctv_task_time_picker); taskTimePicker.setIs24HourView(真正的); SET按钮(DialogInterface.BUTTON_POSITIVE,选择,新DialogInterface.OnClickListener(){ @覆盖 公共无效的onClick(DialogInterface对话,诠释它){ INT小时= taskTimePicker.getCurrentHour(); INT分钟= taskTimePicker.getCurrentMinute(); 日历CAL =新的GregorianCalendar(); cal.set(0,0,0,时,分,0); ((AddTaskViewActivity)上下文).setSelectedTime(cal.getTime()); } }); SET按钮(DialogInterface.BUTTON_NEUTRAL,没时间,新DialogInterface.OnClickListener(){ @覆盖 公共无效的onClick(DialogInterface对话,诠释它){ ((AddTaskViewActivity)上下文).setSelectedTime(空); } }); } }
XML:
< RelativeLayout的的xmlns:机器人=http://schemas.android/apk/res/android 的xmlns:工具=http://schemas.android/tools 机器人:layout_width =match_parent 机器人:layout_height =match_parent> < TimePicker 机器人:ID =@ + ID / dctv_task_time_picker 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_alignParentTop =真 机器人:layout_centerHorizontal =真/> < / RelativeLayout的>在我搬到了initialiation到的onCreate并使用的setContentView(查看),在对话框的背景是透明的,它的全屏,并没有buttons..why?
解决方案我可以使用timePicker.setCurrentHour(新的Date()。getHours()),但是这不是我想要的解决方案。它应该是可能的解决它在一个更好的方法。
纵观source code ,这是出现一段时间后,API 10的一个错误:
公共无效setIs24HourView(布尔is24HourView){ 如果(mIs24HourView == is24HourView){ 返回; } mIs24HourView = is24HourView; //缓存因为微调范围变化的当前小时 INT currentHour的= getCurrentHour(); updateHourControl(); //后的微调范围设定值更新 setCurrentHour(currentHour的); updateAmPmControl(); }
正确的顺序是:
//缓存,因为微调范围变化的当前小时 INT currentHour的= getCurrentHour(); mIs24HourView = is24HourView;由于 getCurrentHour()依赖于 mIs24HourView 返回1-12或0-23 ...
但直到源$ C $ C更新和部署,你没有多少选择的余地。你需要调用 setCurrentHour()在 setIs24HourView()自己。
For example when I create this timePicker(below) at 18:00, the timePicker's timeValue will be 06:00. At 19:44->07:44... So it doesn't move to the correct current hour after switching from AM/PM to 24h mode. How can I change that? My goal is that the timePicker shows the current time when it was created. Like it does when I use it in AM/PM mode.
TimePicker timePicker = (TimePicker) convertView.findViewById(R.id.time_picker); timePicker.setIs24HourView(true);Sry for the bad formulation, hope you can understand my question. Otherwise just ask.
UPDATE:
public class ChooseTimeViewDialog extends AlertDialog { protected Context context; public ChooseTimeViewDialog(final Context context) { super(context); this.context = context; } public void onCreate(Bundle savedInstanceState) { LayoutInflater inflater = this.getLayoutInflater(); View convertView = inflater.inflate(R.layout.dialog_choose_time_view, null); setContentView(convertView); setTitle(R.string.title_dialog_choose_time_view); final TimePicker taskTimePicker = (TimePicker) convertView.findViewById(R.id.dctv_task_time_picker); taskTimePicker.setIs24HourView(true); setButton(DialogInterface.BUTTON_POSITIVE, "Choose", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int hour = taskTimePicker.getCurrentHour(); int minute = taskTimePicker.getCurrentMinute(); Calendar cal = new GregorianCalendar(); cal.set(0, 0, 0, hour, minute, 0); ((AddTaskViewActivity) context).setSelectedTime(cal.getTime()); } }); setButton(DialogInterface.BUTTON_NEUTRAL, "No time", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ((AddTaskViewActivity) context).setSelectedTime(null); } }); } }XML:
<RelativeLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TimePicker android:id="@+id/dctv_task_time_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout>After I moved the initialiation to onCreate and used the setContentView(View), the background of the dialog is transparent, it's fullscreen and has no buttons..why?
解决方案I can use timePicker.setCurrentHour(new Date().getHours()), but that's not the solution I want. It should be possible to solve it in a better way.
Looking at the source code, this is a bug that appeared sometime after API 10:
public void setIs24HourView(Boolean is24HourView) { if (mIs24HourView == is24HourView) { return; } mIs24HourView = is24HourView; // cache the current hour since spinner range changes int currentHour = getCurrentHour(); updateHourControl(); // set value after spinner range is updated setCurrentHour(currentHour); updateAmPmControl(); }The correct order is:
// cache the current hour since spinner range changes int currentHour = getCurrentHour(); mIs24HourView = is24HourView;Because getCurrentHour() relies on mIs24HourView to return 1-12 or 0-23...
But until the source code is updated and deployed, you don't have much of a choice. You need to call setCurrentHour() after setIs24HourView() yourself.
更多推荐
切换到24小时后TimePicker小时不更新
发布评论