如何在警报对话框中添加复选框?

时间:2019-06-24 09:25:10

标签: java android

final AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
               // View mView = getLayoutInflater().inflate(R.layout.dialog_checkbox, null);
              //  CheckBox mCheckBox = mView.findViewById(R.id.checkBox);

                builder.setTitle("Agree Term and Policy");
                builder.setMessage("Legit’ However, ‘EXPERIENCES’ help;");
                builder.setPositiveButton("Continue",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });

                builder.show();

                LayoutInflater eulaInflater = LayoutInflater.from(Login.this);
                View eulaLayout = eulaInflater.inflate(R.layout.dialog_checkbox, null);
                builder.setView(eulaLayout);
                CheckBox dontShowAgain = (CheckBox)eulaLayout.findViewById(R.id.checkBox);

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:padding="10dp">

  <CheckBox
  android:id="@+id/checkBox"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Don't show this message again" />
</LinearLayout>

这是dialog_checkbox的布局。我可以在对话框内的setnegativebutton位置添加复选框吗?我尝试了很多事情,但没有任何效果,并且在alertdialog中不显示复选框。

2 个答案:

答案 0 :(得分:0)

在设置视图之前显示对话框,这样您将获得默认对话框,在设置视图之后显示对话框。还要保持textview以显示消息和按钮以继续。

final AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);


                LayoutInflater eulaInflater = LayoutInflater.from(Login.this);
                View eulaLayout = eulaInflater.inflate(R.layout.dialog_checkbox, null);
                builder.setView(eulaLayout);
                CheckBox dontShowAgain = (CheckBox)eulaLayout.findViewById(R.id.checkBox);


                builder.show();

答案 1 :(得分:0)

AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
        mBuilder.setTitle(title_default);
        mBuilder.setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int position, boolean isChecked) {
                if(isChecked){
                    mUserItems.add(position);
                }else{
                    mUserItems.remove((Integer.valueOf(position)));
                }
            }
        });

        mBuilder.setCancelable(false);
        mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                String item="";
                String no="";
                for (int i = 0; i < mUserItems.size(); i++) {
                    item = item + listItems[mUserItems.get(i)];
                    no = no + mUserItems.get(i);
                    if (i != mUserItems.size() - 1) {
                        item = item + ", ";
                        no = no + ", ";
                    }
                }
                button.setText(item);
            }
        });

        mBuilder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });

        AlertDialog mDialog = mBuilder.create();
        mDialog.show();

您可以检查我的图书馆的完整实施情况。 PrintArray