在Android HoneyComb TimePickerDialog中实现OnTimeChangedListener以设置timeduration

时间:2012-03-28 19:27:28

标签: android android-widget


谁能帮助我如何在Android 3.0的TimePickerDialog中实现OnTimeChangedListener接口。我无法在TimePickerDialog中获得timepicker的引用。 任何人都可以帮我解决这个问题。
在此先感谢,

1 个答案:

答案 0 :(得分:0)

我已回答提出类似问题 here

使用 TimePickerDialog ,这是我的代码

的活动:

public class MainActivity extends Activity {

    private static final int DIALOG_TIME_PICKER = 1;

    private int hours = 10; 
    private int minutes = 30;

    private TextView textViewTimeResult;

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public void setMinutes(int minutes) {
        this.minutes = minutes;
    }

        @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            textViewTimeResult = (TextView) findViewById(R.id.activity_main_textview_timeresult);
    }

    public void onTimeResultClick(View view) {
            showDialog(DIALOG_TIME_PICKER);
    }

        @Override
    protected Dialog onCreateDialog(int dialogId) {

            if (dialogId == DIALOG_TIME_PICKER) {
                    TimePickerDialog timePickerDialog = 
                            new TimePickerDialog(this, timePickerDialogCallBack, hours, minutes, true);
                    return timePickerDialog;
            }
            return super.onCreateDialog(dialogId);
    }

    OnTimeSetListener timePickerDialogCallBack = new OnTimeSetListener() {
            @Override
        public void onTimeSet(TimePicker timePickerView, int hoursValue, int minutesValue) {
                    setHours(hoursValue);
                    setMinutes(minutesValue);
                    textViewTimeResult.setText(hoursValue + " : " + minutesValue);
    }
};

}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/activity_main_textview_timeresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:onClick="onTimeResultClick"
        android:padding="@dimen/padding_medium"
        android:text="click me to choose time!" />

</LinearLayout>

希望它有所帮助!