Andrews radio dialog with the OK and Cancel buttons

Andrews radio dialog easily written:

//...
AlertDialog dialog = builder.setTitle(R.string.please_choose)
                .setSingleChoiceItems(itemStrs, chooseItemIndex, new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //...
                            }
                        }
                )
                .show();

Ok.

In setSingleChoiceItems, the third parameter callback which, under standard index that is to select the radio button. In this callback processing, is to click on, immediately make the appropriate changes, like this lack the "cancel", "go back" process.
But in fact, this way is sufficient. Increase the OK and Cancel buttons, but the process has become cumbersome.

FIG encapsulates the gender selection a dialog box:

Write pictures described here

The complete code is as follows:


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog.Builder;


/**
 * 性别选择对话框
 * Created by lu on 2017/9/1 15:42
 */
public class ChooseSexDialog {

    /**
     * @param sex 初始时的性别。0女,1男
     * @param listener 确定按钮监听。
     */
    public static Dialog show(Context context, int sex, final OnClickOkListener listener){

        Builder builder = new Builder(context);

        //下标正好对应性别的值
        String[] itemStrs = new String[]{
                context.getString(R.string.girl),
                context.getString(R.string.boy)
        };
        if (sex > 1) {
            sex = 1;
        }
        final int chooseItemIndex = sex;
        AlertDialog dialog = builder.setTitle(R.string.please_choose)
                .setSingleChoiceItems(itemStrs, chooseItemIndex, new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (listener != null){
                                    listener.sexValue = which;
                                    listener.isItemClick = true;
                                }
                            }
                        }
                )
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(R.string.ok, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (listener!=null){
                            if (listener.isItemClick){//当点击选择过
                                listener.onPressOK(dialog,listener.sexValue);
                            }else {//未选择过,直接返回上一次选择的item的下标。
                                listener.onPressOK(dialog,chooseItemIndex);
                            }
                        }
                        dialog.dismiss();
                    }
                }).show();
        return dialog;
    }
    public static abstract class OnClickOkListener{
        private int sexValue;
        public boolean isItemClick = false;

        public abstract void onPressOK(DialogInterface alertDialog, int sexValue);

    }
}

——end

Published 105 original articles · won praise 70 · views 340 000 +

Guess you like

Origin blog.csdn.net/Mingyueyixi/article/details/77852718