我正在扩展DialogPreference以获取以下对话框:
我希望它能够选择两次:开始时间和结束时间(无论出于何种原因)。为此,TimePicker应始终在已检查的ToggleButton上显示时间。
为了让用户知道他当前正在改变的两次中的哪一次,我使用ToggleButtons并确保通过OnCheckedChangeListeners一直检查一个。到目前为止没问题。相同的侦听器用于更改那些ToggleButtons上的文本(通过setTextOn / setTextOff方法)。
请参阅我在扩展DialogPreference中使用的以下代码:
/*
* Bind data to our content views
* */
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
// use 24h format
mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
mTimePicker.setIs24HourView(true);
mToggleButtonFrom = (ToggleButton) view.findViewById(R.id.toggleButtonDisplayOnFrom);
mToggleButtonUntil = (ToggleButton) view.findViewById(R.id.toggleButtonDisplayOnUntil);
// save the time of the timepicker internally for both buttons, if there are no saved values
if(fromHour == -1 && fromMin == -1) {
fromHour = mTimePicker.getCurrentHour();
fromMin = mTimePicker.getCurrentMinute();
}
if(untilMin == -1 && untilHour == -1){
untilHour = mTimePicker.getCurrentHour();
untilMin = mTimePicker.getCurrentMinute();
}
// set currently saved times
mToggleButtonFrom.setTextOn(String.format("%02d", fromHour) + ":" + String.format("%02d", fromMin));
mToggleButtonFrom.setTextOff(String.format("%02d", fromHour) + ":" + String.format("%02d", fromMin));
mToggleButtonUntil.setTextOn(String.format("%02d", untilHour) + ":" + String.format("%02d", untilMin));
mToggleButtonUntil.setTextOff(String.format("%02d", untilHour) + ":" + String.format("%02d", untilMin));
// update the button (not working without this)
mToggleButtonFrom.setChecked(mToggleButtonFrom.isChecked());
mToggleButtonUntil.setChecked(mToggleButtonUntil.isChecked());
// set togglebuttons to exclude each other
mToggleButtonFrom.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
if (mToggleButtonUntil.isChecked())
mToggleButtonUntil.setChecked(false);
} else {
// The toggle is disabled
if (!mToggleButtonUntil.isChecked())
mToggleButtonUntil.setChecked(true);
fromHour = getCurrentlyPickedHour2();
fromMin = getCurrentlyPickedMinute2();
String time = getStringTime(fromHour, fromMin);
mToggleButtonFrom.setTextOff(time);
mToggleButtonFrom.setTextOn(time);
setTimeToTimePickerSlim(false);
}
}
});
mToggleButtonUntil.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
if (mToggleButtonFrom.isChecked())
mToggleButtonFrom.setChecked(false);
} else {
// The toggle is disabled
if (!mToggleButtonFrom.isChecked())
mToggleButtonFrom.setChecked(true);
untilHour = getCurrentlyPickedHour2();
untilMin = getCurrentlyPickedMinute2();
String time = getStringTime(untilHour, untilMin);
mToggleButtonUntil.setTextOff(time);
mToggleButtonUntil.setTextOn(time);
setTimeToTimePickerSlim(true);
}
}
});
}
private void setTimeToTimePickerSlim(boolean from){
if(from){
mTimePicker.setCurrentHour(Integer.valueOf(fromHour));
mTimePicker.setCurrentMinute(Integer.valueOf(fromMin));
} else {
mTimePicker.setCurrentHour(Integer.valueOf(untilHour));
mTimePicker.setCurrentMinute(Integer.valueOf(untilMin));
}
}
private int fromHour = -1;
private int fromMin = -1;
private int untilHour = -1;
private int untilMin = -1;
private int getCurrentlyPickedHour2(){
return mTimePicker.getCurrentHour();
}
private int getCurrentlyPickedMinute2(){
return mTimePicker.getCurrentMinute();
}
private String getStringTime(int hour, int min){
return String.format("%02d", hour)+":"+String.format("%02d", min);
}
我绝对没有任何线索,为什么TimePicker没有更新到显示的(和设置)时间,当我选择一个新的时间然后点击其中一个ToggleButtons时(尽我所能,两者都有相同的行为)告诉)。
但现在最有趣的部分是:当我这样做两次(选择一个新时间,切换按钮,选择另一个时间,再次切换)时,TimePicker也会开始切换显示的时间。正是我想要它展示的!
我尝试了很多东西,这是我最好的'猜测:
如果我将方法setTimeToTimePickerSlim(boolean)更改为
private void setTimeToTimePickerSlim(boolean from){
if(from){
mTimePicker.setCurrentHour(3);
mTimePicker.setCurrentMinute(15);
} else {
mTimePicker.setCurrentHour(21);
mTimePicker.setCurrentMinute(45);
}
}
这样的工作就像预期的那样(但是有点静态才有用...)。不幸的是,删除Integer.valueOf()也无济于事。
我会非常高兴任何提示(因为我现在试图调试这几个小时)。
更新:我通过以下修改记录了值
private void setTimeToTimePickerSlim(boolean from){
Log.i("StrangeBugTest", "Before setting: from: " + fromHour + ":" + fromMin + " until: " + untilHour + ":" + untilMin + " (from=" + from + ")");
Log.i("StrangeBugTest", "Pickertime: " + mTimePicker.getCurrentHour() + ":" + mTimePicker.getCurrentMinute());
if(from){
mTimePicker.setCurrentHour(fromHour);
mTimePicker.setCurrentMinute(fromMin);
} else {
mTimePicker.setCurrentHour(untilHour);
mTimePicker.setCurrentMinute(untilMin);
}
Log.i("StrangeBugTest", "After setting: from: " + fromHour + ":" + fromMin + " until: " + untilHour + ":" + untilMin + " (from=" + from + ")");
Log.i("StrangeBugTest", "Pickertime: " + mTimePicker.getCurrentHour() + ":" + mTimePicker.getCurrentMinute());
}
当我启动应用程序(并打开preferenceDialog)时,我会看到以下屏幕:
我选择21小时(因为我使用的是24小时格式)和分钟45分。之后我点击了toggleButtonFrom(左侧)。这让我得到以下日志(缩短):
这是错误的,因为pickertime应该是设置后的19:42。然而,gui与选择的时间一致:
我将Picker改回小时,选择15小时后跟15分钟,然后点击ToggleButtonUntil(右边一个)。这给了我以下日志
这是从一开始就应该如何运作的! GUI显示:
从这里开始,一切都很好,时间和按钮按预期切换。但我不明白,为什么第一个不起作用。对于我能得到的每一个提示,我都会感到高兴。