Create AlertDialog with both MultiChoiceItems and

2019-08-08 21:54发布

I'm trying to create an AlertDialog where both a message (in a TextView, for instance) and a MultiChoice-list can be displayed at the same time, but I'm a bit lost as to how to do it.

Will I have to create my own subclass of AlertDialog, or is there an easier way to do it?

2条回答
forever°为你锁心
2楼-- · 2019-08-08 22:23

Use DialogFragment for customization

    public class ResponseDialog extends DialogFragment implements View.OnClickListener{

    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        getDialog().getWindow()
                .getAttributes().windowAnimations = R.style.DialogAnimationFromDown;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Black_NoTitleBar);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return dialog;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT, null);
        //implement in layout what you want 
        return v;
    }

    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
    }
    }
}

for more info https://developer.android.com/reference/android/app/DialogFragment.html

查看更多
看我几分像从前
3楼-- · 2019-08-08 22:29

You could create a normal AlertDialog but, instead of using the setMessage() method, you could just use a custom layout (you can inflate it in a View or even create a custom view) and use then the setView() method of the AlertDialog class.

查看更多
登录 后发表回答