How to prevent a dialog from closing when a button is clicked

后端 未结 18 2166
无人及你
无人及你 2020-11-21 23:59

I have a dialog with EditText for input. When I click the \"yes\" button on dialog, it will validate the input and then close the dialog. However, if the input

18条回答
  •  萌比男神i
    2020-11-22 00:44

    It could be built with easiest way:

    Alert Dialog with Custom View and with two Buttons (Positive & Negative).

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setTitle(getString(R.string.select_period));
    builder.setPositiveButton(getString(R.string.ok), null);
    
     builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
    
        // Click of Cancel Button
    
       }
     });
    
      LayoutInflater li = LayoutInflater.from(getActivity());
      View promptsView = li.inflate(R.layout.dialog_date_picker, null, false);
      builder.setView(promptsView);
    
      DatePicker startDatePicker = (DatePicker)promptsView.findViewById(R.id.startDatePicker);
      DatePicker endDatePicker = (DatePicker)promptsView.findViewById(R.id.endDatePicker);
    
      final AlertDialog alertDialog = builder.create();
      alertDialog.show();
    
      Button theButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
      theButton.setOnClickListener(new CustomListener(alertDialog, startDatePicker, endDatePicker));
    

    CustomClickLister of Positive Button of Alert Dailog:

    private class CustomListener implements View.OnClickListener {
            private final Dialog dialog;
            private DatePicker mStartDp, mEndDp;
        public CustomListener(Dialog dialog, DatePicker dS, DatePicker dE) {
            this.dialog = dialog;
            mStartDp = dS;
            mEndDp = dE;
        }
    
        @Override
        public void onClick(View v) {
    
            int day1  = mStartDp.getDayOfMonth();
            int month1= mStartDp.getMonth();
            int year1 = mStartDp.getYear();
            Calendar cal1 = Calendar.getInstance();
            cal1.set(Calendar.YEAR, year1);
            cal1.set(Calendar.MONTH, month1);
            cal1.set(Calendar.DAY_OF_MONTH, day1);
    
    
            int day2  = mEndDp.getDayOfMonth();
            int month2= mEndDp.getMonth();
            int year2 = mEndDp.getYear();
            Calendar cal2 = Calendar.getInstance();
            cal2.set(Calendar.YEAR, year2);
            cal2.set(Calendar.MONTH, month2);
            cal2.set(Calendar.DAY_OF_MONTH, day2);
    
            if(cal2.getTimeInMillis()>=cal1.getTimeInMillis()){
                dialog.dismiss();
                Log.i("Dialog", "Dismiss");
                // Condition is satisfied so do dialog dismiss
                }else {
                Log.i("Dialog", "Do not Dismiss");
                // Condition is not satisfied so do not dialog dismiss
            }
    
        }
    }
    

    Done

提交回复
热议问题