1

所以在我的应用程序的一个页面上,有两个 EditText 字段(editEarliestDepart 和 editLatestDepart)。

我希望发生两件事:

  1. 当用户单击其中一个时,我希望出现一个 TimePicker 片段对话框。用户设置时间后,这些值将被传递并显示在刚刚选择的 EditText 字段中。

  2. 当用户设置“editLatestDepart”时,确保“editLatestDepart”总是在“editEarliestDepart”之后。

谁能帮我解决这个问题?我已经尝试了几天,这让我发疯。

谢谢,

凯文

调用 Timepicker 片段对话框的 Activity。

public class NewDriverSetupTime extends FragmentActivity implements TimePickedListener   {

    String pickerHour, pickerMinute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_driver_setup_time);

        //create on click listener for earliest depart
        EditText earliestDepart = (EditText) findViewById(R.id.editEarliestDepart);
        earliestDepart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment newFragment = new TimePickerDialogFragment();
                newFragment.show(getFragmentManager(), "timePicker");   
            }
        });

        //create on click listener for latest depart
        EditText latestDepart = (EditText) findViewById(R.id.editLatestDepart);
        latestDepart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment newFragment = new TimePickerDialogFragment();
                newFragment.show(getFragmentManager(), "timePicker");
            }
        });

    }

    @Override
    public void onTimePicked(Calendar time) {
        // TODO Auto-generated method stub
        // display the selected time in the clicked editview

    }
}

这是实际的片段代码

public class TimePickerDialogFragment extends DialogFragment
    implements TimePickerDialog.OnTimeSetListener {

    private TimePickedListener mListener;

    @Override

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        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,
                DateFormat.is24HourFormat(getActivity()));
    }
     @Override
     public void onAttach(Activity activity)  {
         // when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
         super.onAttach(activity);
         try
         {
             mListener = (TimePickedListener) activity;
         }
         catch (ClassCastException e)
         {
             throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
         }

     }

     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        // when the time is selected, send it to the activity via its callback interface method
            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
            c.set(Calendar.MINUTE, minute);

            mListener.onTimePicked(c);

        }
     public static interface TimePickedListener
        {
            public void onTimePicked(Calendar time);
        }
    }
4

2 回答 2

3

这可能会帮助你...

在 onclick 方法中....

Calendar mcurrentTime = Calendar.getInstance();
        int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
        int minute = mcurrentTime.get(Calendar.MINUTE);
        TimePickerDialog mTimePicker;
        mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                edittext.setText( selectedHour + ":" + selectedMinute);
            }
        }, hour, minute, true);//Yes 24 hour time
        mTimePicker.setTitle("Select Time");
        mTimePicker.show();
于 2013-10-08T13:52:09.800 回答
0

你想要做的是设计一个里面有你的 TimePicker 的片段。然后在这个片段内部有一个回调到您的主要活动,它填充主要活动 EditText。

您可以在此处找到有关使用回调的信息:

与其他 Fragment 通信

希望这对您的第一个问题有所帮助,当我有更多时间解决 #2 时,我可能会进行编辑。

于 2013-10-11T14:19:54.837 回答