2016-05-26 67 views
0

使用android datepicker時,我想始終顯示舊的日期月份微調而不是新的日曆視圖?我怎樣才能做到這一點?Android日期選擇器 - 總是顯示日期月份的微調器佈局而不是日曆視圖

我想這對於運行任何Android版本的所有設備: enter image description here 和不想如下界面:

enter image description here

+0

如何通過編程或xml將日期選擇器添加到您的應用程序? – Dhiraj

+1

[在API 21中以編程方式從DatePicker中隱藏日曆視圖]的可能重複(http://stackoverflow.com/questions/27709668/hide-calenderview-from-datepicker-programatically-in-api-21) –

回答

8

我使用過這個答覆https://stackoverflow.com/a/31610267/5700434徹底解決問題。

主要使用:

DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day); 
     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

全碼:

import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.graphics.Color; 
import android.graphics.drawable.ColorDrawable; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.widget.DatePicker; 

import java.util.Calendar; 


public class DatePickerFragment extends DialogFragment 
     implements DatePickerDialog.OnDateSetListener { 

    private Calendar calendar; 
    private Calendar maxCalendar; 
    private Calendar minCalendar; 
    private DatePickerDialog.OnDateSetListener onDateSetListener; 

    private static final String TAG = DatePickerFragment.class.getSimpleName(); 

    public void setup(Calendar calendar, DatePickerDialog.OnDateSetListener onDateSetListener){ 
     this.calendar = calendar; 
     this.onDateSetListener = onDateSetListener; 
    } 

    public void setup(Calendar calendar, Calendar maxCalendar, DatePickerDialog.OnDateSetListener onDateSetListener){ 
     this.calendar = calendar; 
     this.maxCalendar = maxCalendar; 
     this.onDateSetListener = onDateSetListener; 
    } 

    public void setup(Calendar calendar, Calendar maxCalendar, Calendar minCalendar){ 
     this.calendar = calendar; 
     this.maxCalendar = maxCalendar; 
     this.minCalendar = minCalendar; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     int year = calendar.get(Calendar.YEAR); 
     int month = calendar.get(Calendar.MONTH); 
     int day = calendar.get(Calendar.DAY_OF_MONTH); 

     DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day); 
     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

     if(minCalendar!=null){ 
      dialog.getDatePicker().setMinDate(minCalendar.getTimeInMillis()); 
     } 

     if(maxCalendar != null){ 
      dialog.getDatePicker().setMaxDate(maxCalendar.getTimeInMillis()); 
     } 

     return dialog; 
    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 
     if(onDateSetListener != null){ 
      onDateSetListener.onDateSet(view, year, month, day); 
     }else{ 
      LogUtils.w(TAG, "onDateSetListener callback is not initialized."); 
     } 
    } 
} 
1

有可用於Android日期選擇器很多圖書館(如:exmple1example2example3或你可以使用默認日期選擇器This日期選擇器可能是你正在尋找的那個

0
DatePickerDialog dialog = new 
DatePickerDialog(getActivity(),AlertDialog.THEME_HOLO_LIGHT, this, year, month, day); 

這在Android中的所有版本的完美。從4.4到6.0 進行了測試,雖然它已被棄用,但它支持得很好。

相關問題