简体   繁体   中英

Custom alert dialog not dismiss

I want to implement dismiss to R.id.textSizeA button, but it's not working. Anyone can help? The custom alert dialog has lots of radio buttons for its settings. When I press the radio button it should close the dialog, but I don't know how to do.

 final LinearLayout textSizeBtn = (LinearLayout) findViewById(R.id.text_size_btn);
    textSizeBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View view) {

            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(view.getContext());

            final LayoutInflater layoutInflater = LayoutInflater.from(view.getContext());
            final View view1 = layoutInflater.inflate(R.layout.font_size_dialog, null);
            alertDialog.setView(view1);


            RadioGroup radioGroup = (RadioGroup) view1.findViewById(R.id.textSizeChanGroup);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                    TextView sampleText = (TextView) view1.findViewById(R.id.texSize_sampleText);

                    if (checkedId == R.id.textSizeA) {
                        String size = String.valueOf(12);
                        sampleText.setTextSize(Float.parseFloat(size));
                        textSizeHint.setText(size);
                        SharedPreferences.Editor editor = view.getContext().getSharedPreferences(String.valueOf(R.string.textSizePref), MODE_PRIVATE).edit();
                        editor.putString("textSizeA", size);
                        editor.clear();
                        editor.apply();

                    } else if (checkedId == R.id.textSizeB) {
                        String size = String.valueOf(13);
                        sampleText.setTextSize(Float.parseFloat(size));
                        textSizeHint.setText(size);
                        SharedPreferences.Editor editor = view.getContext().getSharedPreferences(String.valueOf(R.string.textSizePref), MODE_PRIVATE).edit();
                        editor.putString("textSizeA", size);
                        editor.clear();
                        editor.apply();

                    }
                }
            });
            alertDialog.create().show();

        }
          });

Try this

Change

alertDialog.create().show();

to

AlertDialog alert = alertDialog.create();
alert.show();

now use alert to dismiss the alert like below

if (checkedId == R.id.textSizeA) {
    String size = String.valueOf(12);
    ...
    ...
    editor.apply();
    alert.dismiss(); // add this
} else if
...
...

Replace this line

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(view.getContext());

with

final AlertDialog alertDialog = new AlertDialog.Builder(view.getContext());

And this

alertDialog.create().show();

with

alertDialog.show(); and call alertDialog.dismiss(); whenever you want to dismiss alert dialog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM