如何创建TimePickerDialog android

时间:2017-08-19 05:42:15

标签: java android dialog datetimepicker

我想创建TimePickerDialog,主题是示​​例对话框。

但是我按照这个链接

  

http://www.android-examples.com/create-timepickerdialog-to-select-time-in-12-hours-format-android/

结果不是简单的对话

结果是

enter image description here

我想要

enter image description here

如何设置?

抱歉我的形象太大了。

6 个答案:

答案 0 :(得分:9)

我为旧的TimePickerDialog做了一个方法......我和你一样有同样的情况......这对我很有用..

使用以下方法

    public void showHourPicker() {
            final Calendar myCalender = Calendar.getInstance();
            int hour = myCalender.get(Calendar.HOUR_OF_DAY);
            int minute = myCalender.get(Calendar.MINUTE);


            TimePickerDialog.OnTimeSetListener myTimeListener = new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    if (view.isShown()) {
                        myCalender.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        myCalender.set(Calendar.MINUTE, minute);

                    }
                }
            };
            TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar, myTimeListener, hour, minute, true);
            timePickerDialog.setTitle("Choose hour:");
  timePickerDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            timePickerDialog.show();
        }

希望这会对你有所帮助......如果你需要任何帮助,请告诉我

答案 1 :(得分:2)

尝试以下代码。它看起来几乎与您提供的图像相似

<TimePicker
    android:id="@+id/simpleTimePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:timePickerMode="spinner"/>

enter image description here

答案 2 :(得分:0)

您可以尝试 datePickerMode spinner ,如下面的日期时间选择器代码

android:calendarViewShown="false"
android:datePickerMode="spinner"

答案 3 :(得分:0)

更改此行:

timepickerdialog = new TimePickerDialog(MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {

到此:

timepickerdialog = new TimePickerDialog(MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK,
                    new TimePickerDialog.OnTimeSetListener() {

它允许您主题化TimePicker

答案 4 :(得分:0)

自定义TimePicker:

您可以创建一个DateTimePickerCustom类,其中包含您可以在整个项目中使用的方法:

    public static void getTimePicker(Context context, final int position, final PickerClickListener pickerClickListener) {  
          final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.dialog_time);
            dialog.show();
            final TimePicker timePicker = (TimePicker) dialog.findViewById(R.id.timePicker);
            timePicker.setIs24HourView(false);
            Button buttonOk = (Button) dialog.findViewById(R.id.buttonOk);
            Button buttonCancel = (Button) dialog.findViewById(R.id.buttonCancel);
            buttonCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            buttonOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        SimpleDateFormat sdf24 = new SimpleDateFormat("HH:mm");
                        SimpleDateFormat sdf12 = new SimpleDateFormat("hh:mm a");

                        String strTime = timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute();
                        Date time = sdf24.parse(strTime);
                        String formattedTime = sdf12.format(time);
                        Log.d(TAG, "formattedTime: " + formattedTime);
                        pickerClickListener.onTimeSelect(formattedTime, position);
                        dialog.dismiss();

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }


                }
            });
}

XML 代码:

<EditText
       android:id="@+id/editTextStartTime"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true"
       android:editable="false"
       android:focusable="false"
       android:singleLine="true"
       android:textSize="14sp" />

MainActivity 中将此方法称为:

 @OnClick(R.id.editTextStartTime)
    public void startTimePicker() {
        DateTimePickerCustom.getTimePicker(context, 0, this);
    }

答案 5 :(得分:0)

您可以使用自定义time picker,我有更多自定义here,如S-planner