2013-05-22 3 views
6

मैं सिर्फ एंड्रॉयड एक शौक के रूप में जानने के लिए शुरू कर दिया और मैं दो datepicker के साथ एक संवाद बनाना चाहेंगेमैं दो डेटपिकर के साथ एक कस्टम संवाद कैसे बना सकता हूं?

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

मैं कैसे संवाद से चयनित मानों मिल सकता है? क्या संवाद पर स्वचालित रूप से ठीक/रद्द करें बटन शामिल करने की संभावना है?

क्या ऐसी कोई लाइब्रेरी है जिसमें ऐसी कार्यक्षमता है (प्रारंभ और समाप्ति तिथि/अवधि चयन)?

+0

के लिए एक ही एक संवाद एक्सएमएल में दो दिनांक पिकर जोड़ें। परिवर्तन सुनने के लिए OnDateChangedaListener का उपयोग करें। "रद्द करें" के लिए setNegativeButton का उपयोग करें और "ओके" – Milan

उत्तर

12

से डेटा प्राप्त करें शायद Dialogs और Pickers पहले पढ़ने के लिए सबसे अच्छा है।

कार्यान्वयन के लिए, आपके पास दो बटन हो सकते हैं: एक प्रारंभ तिथि के लिए दिनांक पिकर और अंतिम तिथि के लिए दूसरा दिखा सकता है।

संपादित करें: यदि आप वास्तव में 1 संवाद में 2 दिनांक पिकर्स दिखाना चाहते हैं, तो यहां एक उदाहरण है कि इसे कैसे किया जाए। सबसे पहले, एक कस्टम एक्सएमएल लेआउट बनाएँ।

/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> 

अगला एक संवाद में ऊपर लेआउट का उपयोग करने के लिए है:

// 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() द्वारा इस संवाद दिखा सकते हैं।

+2

के लिए सेट पॉजिटिव बटन, यह एक समाधान भी है, लेकिन एक संवाद में दोनों के अच्छे समाधान हैं। –

+0

एक उदाहरण दिखाने के लिए मेरा जवाब अपडेट किया गया। – Krauxe

+0

यह पूरी तरह से काम करता है। महान उदाहरण। – DroidT

0

बस आप अपने एक्सएमएल लेआउट (data_picker_dialog) में डेट पिकर बनाना चाहते हैं। और अपने आईडी

1

अपने लेआउट

<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> 
संबंधित मुद्दे