如何在对话框弹出窗口中显示datepicker对话框?

时间:2017-05-19 07:06:16

标签: android android-layout android-fragments datepicker

我有一个对话框弹出窗口,

           button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                final Dialog dialog = new Dialog(TravellerDetailsActivity.this);

                dialog.setContentView(R.layout.custom_dialog1);
                dialog.setTitle("Traveler Name");
                final RadioGroup manRadioGroup = (RadioGroup) dialog.findViewById(R.id.manRadioGroup1);
                final EditText firstName = (EditText) dialog.findViewById(R.id.editText3);
                final EditText middleName = (EditText) dialog.findViewById(R.id.editText4);
                final EditText lastName = (EditText) dialog.findViewById(R.id.editText5);
                final EditText age = (EditText) dialog.findViewById(R.id.editText6);
                Button btnSave          = (Button) dialog.findViewById(R.id.save);

                btnSave.setOnClickListener(new View.OnClickListener(){
                                               @Override
                                               public void onClick(View view) {

                                                   if(firstName.getText().toString().equals("")) {
                                                       firstName.setError("Enter first name");
                                                       return;
                                                   }
                                                   if(lastName.getText().toString().equals("")) {
                                                       lastName.setError("Enter last name");
                                                       return;
                                                   }

                                                   if(age.getText().toString().equals("")){
                                                       age.setError("Enter age ");
                                                   }

                                                   if (manRadioGroup.getCheckedRadioButtonId() == -1)
                                                   {
                                                       Toast.makeText(TravellerDetailsActivity.this, "Please select title (Mr. or Mrs or Miss) ", Toast.LENGTH_LONG).show();
                                                       return;
                                                   }

                                                   RadioButton radioButton = (RadioButton) dialog.findViewById(manRadioGroup.getCheckedRadioButtonId());

                                                   et.setText(radioButton.getText().toString()+" "+firstName.getText().toString()+" "+middleName.getText().toString()+" "+lastName.getText().toString());

                                                   Guest guest = new Guest();

                                                   Passengers passengers = new Passengers();
                                                   passengers.setTitle(radioButton.getText().toString());
                                                   passengers.setFirstName(firstName.getText().toString());
                                                   passengers.setLastName(lastName.getText().toString());
                                                   passengers.setIsLeadPax("false");
                                                   passengers.setAge(age.getText().toString());
                                                   passengers.setPaxType("1");
                                                   if(radioButton.getText().toString().equals("Master."))
                                                      passengers.setGender("1");
                                                   else
                                                      passengers.setGender("2");

                                                   passengers.setDateOfBirth("08-04-1993");
                                                   passengers.setFFAirline("");
                                                   passengers.setFFNumber("");
                                                   passengers.setCountryCode(getUserCountry(TravellerDetailsActivity.this));
                                                   passengers.setContactNo(mobile.getText().toString());
                                                   passengers.setAddressLine1("kk nagar");
                                                   passengers.setCity("madurai");
                                                   passengers.setCountryName("India");

                                                   Fare fare = new Fare();
                                                   fare.setBaseFare(1950);
                                                   fare.setTax(440);
                                                   fare.setYQTax(0);
                                                   fare.setAdditionalTxnFeeOfrd(0);
                                                   fare.setAdditionalTxnFeePub(0);
                                                   fare.setPGCharge(0);

                                                   Seat seat = new Seat();
                                                   seat.setCode("");
                                                   seat.setDescription("");

                                                   Meal meal = new Meal();
                                                   meal.setCode("");
                                                   meal.setDescription("");

                                                   guest.setPassengers(passengers);
                                                   guest.setFare(fare);
                                                   guest.setMeal(meal);
                                                   guest.setSeat(seat);

                                                   guests.add(guest);



                                                   dialog.dismiss();
                                               }
                                           }
                );
                Button btnCancel        = (Button) dialog.findViewById(R.id.cancel);
                btnCancel.setOnClickListener(new View.OnClickListener(){
                                                 @Override
                                                 public void onClick(View view) {
                                                     dialog.dismiss();
                                                 }
                                             }
                );
                dialog.show();
            }
        });

我需要在弹出窗口中显示此日期选择器对话框。此弹出窗口具有字段验证。可以在这里添加日期选择器吗?如何在Dialog弹出屏幕中实现接口实现DatePickerDialog.OnDateSetListener。

3 个答案:

答案 0 :(得分:0)

使您想要打开DatePicker对话框的editText全局。

age = (EditText) dialog.findViewById(R.id.editText6);

在活动或片段中声明年龄。

现在在setOnClickListener中,您可以正常打开DatePicker。

答案 1 :(得分:0)

    dialog = new Dialog(TravellerDetailsActivity.this);
    dialog.setContentView(R.layout.custom_dialog);

    dob = (EditText) dialog.findViewById(R.id.editText7);

    dob.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            vv = v;
            Calendar now = Calendar.getInstance();
            DatePickerDialog dpd = DatePickerDialog.newInstance(
                    TravellerDetailsActivity.this,
                    now.get(Calendar.YEAR),
                    now.get(Calendar.MONTH),
                    now.get(Calendar.DAY_OF_MONTH)
            );
            dpd.show(TravellerDetailsActivity.this.getFragmentManager(), "Datepickerdialog");

        }
    });

@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
    String date = year + "-" + (++monthOfYear) + "-" + dayOfMonth;
    dob.setText(date);
}

答案 2 :(得分:0)

当你打开datepicker时使用这段代码,假设点击对话框上的按钮获取打开的datepicker,请查看下面的代码。

    button1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            final Dialog dialog = new Dialog(TravellerDetailsActivity.this);

            dialog.setContentView(R.layout.custom_dialog1);
            dialog.setTitle("Traveler Name");
            final RadioGroup manRadioG

roup = (RadioGroup) dialog.findViewById(R.id.manRadioGroup1);
        final EditText firstName = (EditText) dialog.findViewById(R.id.editText3);
        final EditText middleName = (EditText) dialog.findViewById(R.id.editText4);
        final EditText lastName = (EditText) dialog.findViewById(R.id.editText5);
        final EditText age = (EditText) dialog.findViewById(R.id.editText6);
        final Button btnDatePicker = (Button)dialog.findViewById(R.id.btnDatePocker); 
        Button btnSave          = (Button) dialog.findViewById(R.id.save);
        final String selectedDate =""; // selected date

        // Show a datepicker when the dateButton is clicked
        btnDatePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Calendar now = Calendar.getInstance();
                final Calendar c = Calendar.getInstance();

                DatePickerDialog dpd = new DatePickerDialog(context.getContext(),
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {
                                selectedDate = dayOfMonth + "-"
                                        + (monthOfYear + 1) + "-" + year;
                                btnDatePicker.setText(dayOfMonth + "-"
                                        + (monthOfYear + 1) + "-" + year);

                            }
                        }, c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DATE));
                dpd.show();


            }
        });




        btnSave.setOnClickListener(new View.OnClickListener(){
                                       @Override
                                       public void onClick(View view) {

                                           if(firstName.getText().toString().equals("")) {
                                               firstName.setError("Enter first name");
                                               return;
                                           }
                                           if(lastName.getText().toString().equals("")) {
                                               lastName.setError("Enter last name");
                                               return;
                                           }

                                           if(age.getText().toString().equals("")){
                                               age.setError("Enter age ");
                                           }

                                           if (manRadioGroup.getCheckedRadioButtonId() == -1)
                                           {
                                               Toast.makeText(TravellerDetailsActivity.this, "Please select title (Mr. or Mrs or Miss) ", Toast.LENGTH_LONG).show();
                                               return;
                                           }

                                           RadioButton radioButton = (RadioButton) dialog.findViewById(manRadioGroup.getCheckedRadioButtonId());

                                           et.setText(radioButton.getText().toString()+" "+firstName.getText().toString()+" "+middleName.getText().toString()+" "+lastName.getText().toString());

                                           Guest guest = new Guest();

                                           Passengers passengers = new Passengers();
                                           passengers.setTitle(radioButton.getText().toString());
                                           passengers.setFirstName(firstName.getText().toString());
                                           passengers.setLastName(lastName.getText().toString());
                                           passengers.setIsLeadPax("false");
                                           passengers.setAge(age.getText().toString());
                                           passengers.setPaxType("1");
                                           if(radioButton.getText().toString().equals("Master."))
                                               passengers.setGender("1");
                                           else
                                               passengers.setGender("2");

                                           passengers.setDateOfBirth("08-04-1993");
                                           passengers.setFFAirline("");
                                           passengers.setFFNumber("");
                                           passengers.setCountryCode(getUserCountry(TravellerDetailsActivity.this));
                                           passengers.setContactNo(mobile.getText().toString());
                                           passengers.setAddressLine1("kk nagar");
                                           passengers.setCity("madurai");
                                           passengers.setCountryName("India");

                                           Fare fare = new Fare();
                                           fare.setBaseFare(1950);
                                           fare.setTax(440);
                                           fare.setYQTax(0);
                                           fare.setAdditionalTxnFeeOfrd(0);
                                           fare.setAdditionalTxnFeePub(0);
                                           fare.setPGCharge(0);

                                           Seat seat = new Seat();
                                           seat.setCode("");
                                           seat.setDescription("");

                                           Meal meal = new Meal();
                                           meal.setCode("");
                                           meal.setDescription("");

                                           guest.setPassengers(passengers);
                                           guest.setFare(fare);
                                           guest.setMeal(meal);
                                           guest.setSeat(seat);

                                           guests.add(guest);



                                           dialog.dismiss();
                                       }
                                   }
        );
        Button btnCancel        = (Button) dialog.findViewById(R.id.cancel);
        btnCancel.setOnClickListener(new View.OnClickListener(){
                                         @Override
                                         public void onClick(View view) {
                                             dialog.dismiss();
                                         }
                                     }
        );
        dialog.show();
    }
});