2017-08-19 106 views
1

我想創建TimePickerDialog,主題是樣本對話框。如何創建TimePickerDialog android

但我請點擊此鏈接

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

結果不是簡單的對話

結果

enter image description here

我想

enter image description here

如何設置呢?

對不起我的形象是如此之大。

+0

時間選取器是平臺主題依賴 –

+0

您可能會嘗試找到您的答案[here](http s://stackoverflow.com/questions/29250086/custom-time-picker-dialog-in-android)和[這裏](https://stackoverflow.com/questions/32967424/how-can-i-put-date - 和時間選取器-IN-A-對話功能於機器人)。 – Neck

+0

kitkat版本只會給你預期的輸出。 –

回答

7

我已經作出一個方法爲老TimePickerDialog ......我有一個像you..this同樣的情況對我來說是偉大的工作..

使用以下方法

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(); 
     } 

希望這將幫助你...讓我知道如果您需要任何幫助

+0

謝謝!這是工作 –

+0

@ ARR.s很好...很高興幫助你.. –

+0

@ ARR.s你能告訴我爲什麼你拒絕這個答案嗎? –

0

,你可以嘗試datePickerMode微調如下面的代碼日期時間選擇器下面的代碼

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

嘗試。它看起來幾乎類似於你提供什麼樣的形象

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

enter image description here

+0

你可以添加你自己的完成按鈕。而這不是平臺主題依賴 – QEMU

0

改變這一行:

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

這樣:

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

它可以讓你的主題你TimePicker

0

定製TimePicker:

You ca n請一類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); 
    }