2013-05-22 32 views
6

Tôi chỉ mới bắt đầu học Android như một sở thích và tôi muốn tạo ra một hộp thoại với hai datepickerTôi làm cách nào để tạo hộp thoại tùy chỉnh với hai bộ đếm ngày?

final Dialog dialog = new Dialog(this); 
dialog.setContentView(R.layout.data_picker_dialog); 
dialog.setTitle(R.string.date_period_picker); 
dialog.show(); 
return true; 

Làm thế nào tôi có thể nhận được các giá trị được chọn từ hộp thoại? Có khả năng bao gồm nút OK/Hủy tự động trên hộp thoại không?

Có thư viện nào có chức năng như vậy (Bắt đầu và kết thúc ngày/khoảng thời gian) không?

+0

của bạn Thêm hai chọn ngày trong một xml thoại. Sử dụng OnDateChangedaListener để nghe những thay đổi. Sử dụng setNegativeButton cho "Cancel" và setPositiveButton cho "Ok" – Milan

Trả lời

12

Có lẽ tốt nhất bạn nên đọc khoảng DialogsPickers trước tiên.

Để thực hiện, bạn có thể có hai nút: Một để hiển thị bộ chọn ngày cho ngày bắt đầu và ngày khác cho ngày kết thúc.

Chỉnh sửa: Nếu bạn thực sự muốn hiển thị 2 bộ chọn ngày trong 1 hộp thoại, dưới đây là ví dụ về cách thực hiện. Đầu tiên, tạo một bố trí XML tùy chỉnh.

/res/layout/custom_date_picker.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <DatePicker 
     android:id="@+id/dpStartDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:calendarViewShown="false" /> 

    <DatePicker 
     android:id="@+id/dpEndDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:calendarViewShown="false" /> 

</LinearLayout> 

Tiếp theo là sử dụng bố trí trên trong một hộp thoại:

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

Cuối cùng, bạn có thể hiển thị hộp thoại này bằng cách đơn giản gọi showDatePicker().

+2

Đây cũng là một giải pháp, nhưng giải pháp đẹp hơn để có cả hai trong một hộp thoại. –

+0

Cập nhật câu trả lời của tôi để hiển thị ví dụ. – Krauxe

+0

Điều này hoạt động hoàn hảo. Ví dụ tuyệt vời. – DroidT

0

Chỉ cần bạn muốn tạo bộ chọn ngày cho bố cục xml (data_picker_dialog). Và nhận dữ liệu từ id của bạn

1

Tương tự cho bố trí

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<DatePicker 
     android:id="@+id/datePicker1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

<DatePicker 
     android:id="@+id/datePicker2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 
Các vấn đề liên quan