java - 如何将复选框添加到警报对话框?

标签 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布局。我可以在对话框内添加复选框来设置负按钮吗?我尝试了很多方法都不起作用,但没有在警报对话框中显示复选框。

最佳答案

您在设置 View 之前显示对话框,以便获得默认对话框,在设置 View 之后显示对话框。还保留 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();

关于java - 如何将复选框添加到警报对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56733556/

相关文章:

java - 什么是 GWT 开发者插件协议(protocol)

android - 错误 : Error: No resource found that matches the given name: attr 'colorAccent'

android - 将图像拖动到框的边界 - Jetpack Compose

java - 为什么这段代码用 eclipse 编译器编译而不用 javac (maven)

java - 线程 "main"java.lang.ClassNotFoundException 中的异常,mapreduce

java - 有没有办法在运行时忽略 Jackson 字段而不使用 @JsonIgnore

java - fragment 中的 RecyclerView 从 Activity 返回后为空

android - 如何使用上传 key 为您的应用签名

android - 如何根据EditText属性调整TextView的高度和宽度?

android - 相关微调器 Android