android datepicker 自定义,android – 如何使用两个datepicker创建自定义对话框?
最好先閱讀
Dialogs和
Pickers.
至于實現,您可以有兩個按鈕:一個用于顯示開始日期的日期選擇器,另一個用于顯示結束日期.
編輯:如果你真的想在1個對話框中顯示2個日期選擇器,這里有一個如何做的例子.首先,創建自定義XML布局.
/res/layout/custom_date_picker.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/dpStartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="false" />
android:id="@+id/dpEndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="false" />
接下來是在對話框中使用上面的布局:
// These variables will hold the date values later
private int startYear,startMonth,startDay,endYear,endMonth,endDay;
/**
* Displays the start and end date picker dialog
*/
public void showDatePicker() {
// Inflate your custom layout containing 2 DatePickers
LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_date_picker,null);
// Define your date pickers
final DatePicker dpStartDate = (DatePicker) customView.findViewById(R.id.dpStartDate);
final DatePicker dpEndDate = (DatePicker) customView.findViewById(R.id.dpEndDate);
// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView); // Set the view of the dialog to your custom layout
builder.setTitle("Select start and end date");
builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which) {
startYear = dpStartDate.getYear();
startMonth = dpStartDate.getMonth();
startDay = dpStartDate.getDayOfMonth();
endYear = dpEndDate.getYear();
endMonth = dpEndDate.getMonth();
endDay = dpEndDate.getDayOfMonth();
dialog.dismiss();
}});
// Create and show the dialog
builder.create().show();
}
最后,您只需調用showDatePicker()即可顯示此對話框.
總結
以上是生活随笔為你收集整理的android datepicker 自定义,android – 如何使用两个datepicker创建自定义对话框?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 帝伯朗618新出的氧吧锅还是不粘锅吗?0
- 下一篇: 插桶泵品牌排行榜前十名?