Alert dialog with text followed with a checkbox and 2 buttons

前端 未结 2 1670
长发绾君心
长发绾君心 2021-02-15 12:46

I have requirement to pop up alert dialog which is like a EULA screen. Which will have text describing EULA with a checkbox \"Don\'t show this again\" and in the end 2 buttons f

2条回答
  •  粉色の甜心
    2021-02-15 13:38

    Look at these two different method:

    1) First method more simply and very fast:

    CheckBox checkBox = new CheckBox(this);
    checkBox.setText("This is your checkbox message");
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                            LinearLayout.LayoutParams.FILL_PARENT));
    linearLayout.setOrientation(1);     
    linearLayout.addView(checkBox);
    
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setView(linearLayout);
    alertDialogBuilder.setTitle("This is the title of alert dialog");
    alertDialogBuilder.setMessage("This is the message of alert dialog");
    alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
              // do something
         }
    });
    alertDialogBuilder.show();
    

    2) Second method, with a more customized layout:

    look this page, there is also the XML code to create the checkbox.

提交回复
热议问题