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

后端 未结 18 1868
无人及你
无人及你 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条回答
  • 2020-11-22 00:42

    For pre API 8 i solved the problem using a boolean flag, a dismiss listener and calling dialog.show again if in case the content of the editText wasn´t correct. Like this:

    case ADD_CLIENT:
            LayoutInflater factoryClient = LayoutInflater.from(this);
            final View EntryViewClient = factoryClient.inflate(
                    R.layout.alert_dialog_add_client, null);
    
            EditText ClientText = (EditText) EntryViewClient
                    .findViewById(R.id.client_edit);
    
            AlertDialog.Builder builderClient = new AlertDialog.Builder(this);
            builderClient
                    .setTitle(R.string.alert_dialog_client)
                    .setCancelable(false)
                    .setView(EntryViewClient)
                    .setPositiveButton("Save",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    EditText newClient = (EditText) EntryViewClient
                                            .findViewById(R.id.client_edit);
                                    String newClientString = newClient
                                            .getText().toString();
                                    if (checkForEmptyFields(newClientString)) {
                                        //If field is empty show toast and set error flag to true;
                                        Toast.makeText(getApplicationContext(),
                                                "Fields cant be empty",
                                                Toast.LENGTH_SHORT).show();
                                        add_client_error = true;
                                    } else {
                                        //Here save the info and set the error flag to false
                                        add_client_error = false;
                                    }
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    add_client_error = false;
                                    dialog.cancel();
                                }
                            });
            final AlertDialog alertClient = builderClient.create();
            alertClient.show();
    
            alertClient
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
    
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            //If the error flag was set to true then show the dialog again
                            if (add_client_error == true) {
                                alertClient.show();
                            } else {
                                return;
                            }
    
                        }
                    });
            return true;
    
    0 讨论(0)
  • 2020-11-22 00:42

    you can add builder.show(); after validation message before return;

    like this

        public void login()
    {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(R.layout.login_layout);
        builder.setTitle("Login");
    
    
    
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });// put the negative button before the positive button, so it will appear
    
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                Dialog d = (Dialog) dialog;
                final EditText etUserName = (EditText) d.findViewById(R.id.etLoginName);
                final EditText etPassword = (EditText) d.findViewById(R.id.etLoginPassword);
                String userName = etUserName.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
    
                if (userName.isEmpty() || password.isEmpty())
                {
    
                    Toast.makeText(getApplicationContext(),
                            "Please Fill all fields", Toast.LENGTH_SHORT).show();
                    builder.show();// here after validation message before retrun
                                   //  it will reopen the dialog
                                  // till the user enter the right condition
                    return;
                }
    
                user = Manager.get(getApplicationContext()).getUserByName(userName);
    
                if (user == null)
                {
                    Toast.makeText(getApplicationContext(),
                            "Error ethier username or password are wrong", Toast.LENGTH_SHORT).show();
                    builder.show();
                    return;
                }
                if (password.equals(user.getPassword()))
                {
                    etPassword.setText("");
                    etUserName.setText("");
                    setLogged(1);
                    setLoggedId(user.getUserId());
                    Toast.makeText(getApplicationContext(),
                            "Successfully logged in", Toast.LENGTH_SHORT).show();
                   dialog.dismiss();// if every thing is ok then dismiss the dialog
                }
                else
                {
                    Toast.makeText(getApplicationContext(),
                            "Error ethier username or password are wrong", Toast.LENGTH_SHORT).show();
                    builder.show();
                    return;
                }
    
            }
        });
    
        builder.show();
    
    }
    
    0 讨论(0)
  • 2020-11-22 00:43

    The answer at this link is a simple solution, and which is compatible right back to API 3. It is very similiar to Tom Bollwitt's solution, but without using the less compatible OnShowListener.

    Yes, you can. You basically need to:

    1. Create the dialog with DialogBuilder
    2. show() the dialog
    3. Find the buttons in the dialog shown and override their onClickListener

    I made minor adaptions to Kamen's code since I was extending an EditTextPreference.

    @Override
    protected void showDialog(Bundle state) {
      super.showDialog(state);
    
      class mocl implements OnClickListener{
        private final AlertDialog dialog;
        public mocl(AlertDialog dialog) {
              this.dialog = dialog;
          }
        @Override
        public void onClick(View v) {
    
            //checks if EditText is empty, and if so tells the user via Toast
            //otherwise it closes dialog and calls the EditTextPreference's onClick
            //method to let it know that the button has been pressed
    
            if (!IntPreference.this.getEditText().getText().toString().equals("")){
            dialog.dismiss();
            IntPreference.this.onClick(dialog,DialogInterface.BUTTON_POSITIVE);
            }
            else {
                Toast t = Toast.makeText(getContext(), "Enter a number!", Toast.LENGTH_SHORT);
                t.show();
            }
    
        }
      }
    
      AlertDialog d = (AlertDialog) getDialog();
      Button b = d.getButton(DialogInterface.BUTTON_POSITIVE);
      b.setOnClickListener(new mocl((d)));
    }
    

    Such fun!

    0 讨论(0)
  • 2020-11-22 00:43

    Use a custom layout for your DialogFragment and add a LinearLayout under your content which can be styled as borderless to match Google Material Design. Then find the newly created buttons and override their OnClickListener.

    Example:

    public class AddTopicFragment extends DialogFragment {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            // Get the layout inflater
            LayoutInflater inflater = getActivity().getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.dialog_add_topic, null);
    
            Button saveTopicDialogButton = (Button) dialogView.findViewById(R.id.saveTopicDialogButton);
            Button cancelSaveTopicDialogButton = (Button) dialogView.findViewById(R.id.cancelSaveTopicDialogButton);
    
            final AppCompatEditText addTopicNameET = (AppCompatEditText) dialogView.findViewById(R.id.addTopicNameET);
            final AppCompatEditText addTopicCreatedByET = (AppCompatEditText) dialogView.findViewById(R.id.addTopicCreatedByET);
    
            saveTopicDialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // validate inputs
                    if(addTopicNameET.getText().toString().trim().isEmpty()){
                        addTopicNameET.setError("Topic name can't be empty");
                        addTopicNameET.requestFocus();
                    }else if(addTopicCreatedByET.getText().toString().trim().isEmpty()){
                        addTopicCreatedByET.setError("Topic created by can't be empty");
                        addTopicCreatedByET.requestFocus();
                    }else {
                        // save topic to database
                        Topic topic = new Topic();
                        topic.name = addTopicNameET.getText().toString().trim();
                        topic.createdBy = addTopicCreatedByET.getText().toString().trim();
                        topic.createdDate = new Date().getTime();
                        topic.save();
                        AddTopicFragment.this.dismiss();
                    }
                }
            });
    
            cancelSaveTopicDialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AddTopicFragment.this.dismiss();
                }
            });
    
            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(dialogView)
                   .setMessage(getString(R.string.add_topic_message));
    
            return builder.create();
        }
    
    }
    

    dialog_add_topic.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:errorEnabled="true">
    
            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/addTopicNameET"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Topic Name"
                android:inputType="textPersonName"
                android:maxLines="1" />
    
        </android.support.design.widget.TextInputLayout>
    
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:errorEnabled="true">
    
            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/addTopicCreatedByET"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Created By"
                android:inputType="textPersonName"
                android:maxLines="1" />
    
        </android.support.design.widget.TextInputLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:text="@string/cancel"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cancelSaveTopicDialogButton"
                style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog" />
    
            <Button
                android:text="@string/save"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/saveTopicDialogButton"
                style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog" />
    
        </LinearLayout>
    
    
    </LinearLayout>
    

    This is the final result.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 00:45

    To prevent Dialog box from closing when clicked and it should only close when the internet is available

    I am trying to do the same thing, as I don't want the dialog box to be closed until and unless the internet is connected.

    Here is my code:

    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("Internet Not Connected");
        if(ifConnected()){
    
            Toast.makeText(this, "Connected or not", Toast.LENGTH_LONG).show();
        }
        else{
            builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                   if(!ifConnected())
                   {
                       builder.show();
                   }
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            });
            builder.show();
    
        }
    

    And here is my Connectivity manager code:

     private boolean ifConnected()
    {
        ConnectivityManager connectivityManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
       return networkInfo!=null && networkInfo.isConnected();
    }
    
    0 讨论(0)
提交回复
热议问题