如何阅读TimePicker选择的值?

时间:2013-02-22 15:50:25

标签: android timepicker

我遵循了这个指南:http://developer.android.com/guide/topics/ui/controls/pickers.html 即时通讯我不太确定这是什么地方(我非常喜欢android dev) 我有一个包含mainActivity类和timepicker类的包。 我希望在设置时间时,将其写入mainactivity的变量,或者相反,为此,“onTimeSet”将值保存为类变量,并从mainActivity中读取这些变量。 我该怎么做?

这是我的片段类:

package com.example.remoteswitch;

import java.util.Calendar;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment
                            implements TimePickerDialog.OnTimeSetListener {

    int hour, min;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute, true);
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        hour = hourOfDay;
        min = minute;

    }
}

从我的MainActivity类中,我有一个按下按钮时调用的函数:

public void showTimePickerDialog(View v) {
            //DialogFragment newFragment = new TimePickerFragment();
            TimePickerFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "timePicker"); 

            //todo: read values from timePicker
            int pickerHour = 0, pickerMin = 0;
            //some manipulations on this data
            }
        }

2 个答案:

答案 0 :(得分:12)

<强>重写
我结合了我们讨论过的开发人员指南中的两个例子。现在,您应该能够理解如何将用户从TimePickerFragment中选择的时间发送回更有用的Activity。 (我从第一个例子中做了一些改变。)

TimePickerFragment:

public class TimePickerFragment extends DialogFragment {
    // Notice I removed "implements OnTimeSetListener" and changed the variables

    private Activity mActivity;
    private OnTimeSetListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;

        // This error will remind you to implement an OnTimeSetListener 
        //   in your Activity if you forget
        try {
            mListener = (OnTimeSetListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnTimeSetListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it

        // I made a couple changes here!
        return new TimePickerDialog(mActivity, mListener, hour, minute,
                DateFormat.is24HourFormat(mActivity));
    }
}

MainActivity:

// add "implements OnTimeSetListener" to "public class MainActivity ..."
private int pickerHour = 0;
private int pickerMin = 0;

// onCreate() and your other methods...

public void showTimePickerDialog(View v) {
    TimePickerFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker"); 
}    

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
    pickerHour = hourOfDay;
    pickerMin = minute;
}

答案 1 :(得分:0)

使用以下答案。这对我有用。

接口类:

public interface OnTimeSetListener {
    void onTimeSet(String time);
}

活动类onClick:

        DialogFragment newFragment = new TimePickerFragment(getActivity(), this);
        newFragment.show(getActivity().getSupportFragmentManager(), "timePicker");

覆盖接口侦听器方法:

    @Override
    public void onTimeSet(String time) {
        // TODO Auto-generated method stub
//do something here to set the values
    }