2

我想在偏好活动中有时间选择器。它看起来像这样:

在此处输入图像描述

然后如果用户点击“时间”,它会弹出这个对话框:

在此处输入图像描述

这正是我想要的。但是有几点需要注意:

  1. Dialog 是 XML 引用的我的自定义对话框

      <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

  2. 我的偏好 XML

    <CheckBoxPreference android:title="Checkbox" android:summary="Checkbox"/>
    <EditTextPreference android:summary="time" android:dialogTitle="Hello" android:dialogLayout="@layout/preflayout" />
    

  3. 我的Java代码:

    公共静态类 TestSettings 扩展 PreferenceActivity 实现 TimePicker.OnTimeChangedListener {

        TimePicker tp;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref);
            tp = (TimePicker) findViewById(R.id.timePicker1);
    
        }
    
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
    
            if(view.getId() == R.id.timePicker1) {
            Log.i("STATIC", "hour "+hourOfDay+"minute "+minute);
            }
        }
    
    }
    

出于某种原因,当我设置时间时,OnTimeChangedListener 不会触发。我不知道这是否是在首选项屏幕中以这种方式编程时间选择器的正确方法,但我完全需要它。无论如何,我不知道为什么 OnTimeChangedListener 不起作用。有任何想法吗?

也许我需要在偏好活动中使用另一种方法来编程时间选择器?

提出建议。谢谢!

4

2 回答 2

1

您需要调用setOnTimeChangedListener(this),可能在您的onCreate()方法中,如下所示:

tp.setOnTimeChangedListener(this);
于 2013-03-29T15:37:40.813 回答
1

我必须使用自定义对话框首选项来处理我的首选项活动中的 TimePicker。

TimePickerDialog.java

public class TimePickerDialog extends DialogPreference{
    Context context;
TimePickerDialog timePickerDialog;
TimePicker timePicker;
private final static String TAG = "TIME_DIALOG_PREFERENCE";
private Listener timeListener;
String key;


   public TimePickerDialog (Context context) {
        this(context, null);
        this.context=context;
    }

    public TimePickerDialog (Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context=context;
    }

    public TimePickerDialog (Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);

            timeListener=(Listener) context;
            this.context=context;

        setPositiveButtonText("Set");
        setNegativeButtonText("Cancel");

     }

     public void setListener(Listener listener){
        timeListener =listener;
     }

     @Override
     public View onCreateDialogView(){      
        timePicker =  new TimePicker(context);
        timePicker.setIs24HourView(true);
        return timePicker;
     }

     public void onDialogClosed(boolean positiveResult){
        if(timeListener!=null && positiveResult){
            timeListener.setTime( key,timePicker.getCurrentHour(),timePicker.getCurrentMinute());   
        }
    }


     public interface Listener{
        public void setTime(String key,int hour,int minute);
      }

}

该对话框从 xml 文件首选项布局中调用:

<PreferenceCategory android:title="Schedule" >
    <com.tuxuri.timepicker.TimePickerDialog
        android:defaultValue="08:00"
        android:key="schedule_start_time"
        android:showDefault="true"
        android:summary="When to start"
        android:title="Start time" /> 

</PreferenceCategory>

然后 TimePickerDialog 调用的任何内容都将在 PreferenceActivity 中实现:

 public class UserPreference extends PreferenceActivity implements TimePickerDialog.Listener {

     @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        timePicker = new TimePickerDialog(this);
        addPreferencesFromResource(R.layout.preference);
     }

     @Override
      public void setTime(String key, int hour, int minute) {
            Log.v(TAG,key+"-"+hour+":"+minute);
       }
}
于 2013-06-12T03:28:07.353 回答