EditText上的TimePicker单击

时间:2016-02-23 14:38:10

标签: android timepicker

如何在Timepicker上设置EditText点击android。

我搜索过以下链接,但由于showDialog()现已被删除,因此无法正常工作。

TimePicker Dialog from clicking EditText

http://www.botskool.com/geeks/how-create-time-picker-dialog-selecting-time-android

任何帮助都会得到承认。

3 个答案:

答案 0 :(得分:1)

将此方法用于时间选择器Android开发者网站

    public static class TimePickerFragment extends DialogFragment
                            implements TimePickerDialog.OnTimeSetListener {

    @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,
                DateFormat.is24HourFormat(getActivity()));
    }

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

并称之为你可以这样做

<EditText
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:text="@string/pick_time" 
android:onClick="showTimePickerDialog" />

showTimePickerDialog方法

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

答案 1 :(得分:0)

您必须使用TimePickerDialog类将其显示为Dialog。以下是示例代码:

  TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                }
            }, 0, 0, true);
            timePickerDialog.show();

您可以使用OnClickListener方法

调用此代码

答案 2 :(得分:0)

我得到了同样的答案: -

在片段中包含以下方法。

public void selectTimeToDisplay(EditText edText) {
        DialogFragment dialogFragment = new DisplayTimeFragment(edText);
        dialogFragment.show(getActivity().getSupportFragmentManager(), "DatePicker");
    }

public void setSelectedTime(String hourOfDay,String minute, EditText edText) {
    edText.setText(hourOfDay + ":" + minute);
}

将DisplayTimeFragment定义为片段中的内部类: -

public class DisplayTimeFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
        EditText edText;
        public DisplayTimeFragment(EditText edText){
            this.edText = edText;
        }

        @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);
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            String sHrs, sMins;
            if(minute<10){
                sMins = "0"+minute;
            }else{
                sMins = String.valueOf(minute);
            }
            if(hourOfDay<10){
                sHrs = "0"+hourOfDay;
            }else{
                sHrs = String.valueOf(hourOfDay);
            }
            setSelectedTime(sHrs,sMins, edText);

        }
    }

现在,在Edittext的onClickListener中编写以下代码。

如,

edStartTime.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                selectTimeToDisplay(edStartTime);
            }
        });

注意: - 为了顺利运作,请在focusable to false

xml中设置Edittext.

Bingo ..它现在可以作为任何一个EditText的魅力......