• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

AlertDialog6种使用方法

武飞扬头像
简单点了
帮助2

AlertDialog

1.AlertDialog的6种创建模式

1.1setMessage

1)Java代码

  1.  
    //1.创建构造器
  2.  
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
  3.  
    //2.设置参数
  4.  
    builder.setTitle("弹窗提示")
  5.  
    .setIcon(R.mipmap.boy)
  6.  
    .setMessage("选择你的性别?")
  7.  
    .setPositiveButton("男",new DialogInterface.OnClickListener(){
  8.  
     
  9.  
    @Override
  10.  
    public void onClick(DialogInterface dialogInterface, int i) {
  11.  
    Toast.makeText(DialogTest.this, "选中男", Toast.LENGTH_SHORT).show();
  12.  
    }
  13.  
    })
  14.  
    .setNegativeButton("女",new DialogInterface.OnClickListener(){
  15.  
     
  16.  
     
  17.  
    @Override
  18.  
    public void onClick(DialogInterface dialogInterface, int i) {
  19.  
    Toast.makeText(DialogTest.this, "选中女", Toast.LENGTH_SHORT).show();
  20.  
    }
  21.  
    })
  22.  
    .setNeutralButton("未知",new DialogInterface.OnClickListener(){
  23.  
     
  24.  
    @Override
  25.  
    public void onClick(DialogInterface dialogInterface, int i) {
  26.  
    Toast.makeText(DialogTest.this, "选中未知", Toast.LENGTH_SHORT).show();
  27.  
    }
  28.  
     
  29.  
    });
学新通

学新通

学新通

1.2setItem,设置列表项

1)Java代码

  1.  
    String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
  2.  
    //1.创建构造器
  3.  
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
  4.  
    //2.设置参数
  5.  
    builder.setTitle("请选择城市地区")
  6.  
    .setIcon(R.mipmap.city)
  7.  
    .setItems(citys, new DialogInterface.OnClickListener() {
  8.  
    @Override
  9.  
    public void onClick(DialogInterface dialogInterface, int i) {
  10.  
    Toast.makeText(DialogTest.this, "" citys[i], Toast.LENGTH_SHORT).show();
  11.  
    }
  12.  
    })
  13.  
    .setPositiveButton("确认",new DialogInterface.OnClickListener(){
  14.  
     
  15.  
    @Override
  16.  
    public void onClick(DialogInterface dialogInterface, int i) {
  17.  
     
  18.  
    }
  19.  
    })
  20.  
    .setNegativeButton("取消",new DialogInterface.OnClickListener(){
  21.  
     
  22.  
     
  23.  
    @Override
  24.  
    public void onClick(DialogInterface dialogInterface, int i) {
  25.  
     
  26.  
    }
  27.  
    })
  28.  
    .setNeutralButton("忽略",new DialogInterface.OnClickListener(){
  29.  
     
  30.  
    @Override
  31.  
    public void onClick(DialogInterface dialogInterface, int i) {
  32.  
     
  33.  
    }
  34.  
     
  35.  
    });
  36.  
    //3.创建对象,显示对象
  37.  
    builder.create().show();
学新通

学新通

学新通

1.3setSingleChoiceItems,设置对话框内容为单选列表项

  • 可以传递数组或者是集合
  1.  
    String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
  2.  
    //1.创建构造器
  3.  
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
  4.  
    //2.设置参数
  5.  
    builder.setTitle("请选择城市地区")
  6.  
    .setIcon(R.mipmap.city)
  7.  
    .setSingleChoiceItems(citys, 0,new DialogInterface.OnClickListener() {
  8.  
    @Override
  9.  
    public void onClick(DialogInterface dialogInterface, int i) {
  10.  
    Toast.makeText(DialogTest.this, "" citys[i], Toast.LENGTH_SHORT).show();
  11.  
    }
  12.  
    })
  13.  
    .setPositiveButton("确认",new DialogInterface.OnClickListener(){
  14.  
     
  15.  
    @Override
  16.  
    public void onClick(DialogInterface dialogInterface, int i) {
  17.  
     
  18.  
    }
  19.  
    })
  20.  
    .setNegativeButton("取消",new DialogInterface.OnClickListener(){
  21.  
     
  22.  
     
  23.  
    @Override
  24.  
    public void onClick(DialogInterface dialogInterface, int i) {
  25.  
     
  26.  
    }
  27.  
    })
  28.  
    .setNeutralButton("忽略",new DialogInterface.OnClickListener(){
  29.  
     
  30.  
    @Override
  31.  
    public void onClick(DialogInterface dialogInterface, int i) {
  32.  
     
  33.  
    }
  34.  
     
  35.  
    });
  36.  
    //3.创建对象,显示对象
  37.  
    builder.create().show();
学新通

学新通

1.4setMultiChoiceItems设置多选

  1.  
    // 设置多选
  2.  
    String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
  3.  
    //1.创建构造器
  4.  
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
  5.  
    //2.设置参数
  6.  
    builder.setTitle("请选择城市地区")
  7.  
    .setIcon(R.mipmap.city)
  8.  
    .setMultiChoiceItems(citys, new boolean[]{false, false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
  9.  
    @Override
  10.  
    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
  11.  
    Toast.makeText(DialogTest.this, "" citys[i] b, Toast.LENGTH_SHORT).show();
  12.  
    }
  13.  
    })
  14.  
    .setPositiveButton("确认",new DialogInterface.OnClickListener(){
  15.  
     
  16.  
    @Override
  17.  
    public void onClick(DialogInterface dialogInterface, int i) {
  18.  
     
  19.  
    }
  20.  
    })
  21.  
    .setNegativeButton("取消",new DialogInterface.OnClickListener(){
  22.  
     
  23.  
     
  24.  
    @Override
  25.  
    public void onClick(DialogInterface dialogInterface, int i) {
  26.  
     
  27.  
    }
  28.  
    })
  29.  
    .setNeutralButton("忽略",new DialogInterface.OnClickListener(){
  30.  
     
  31.  
    @Override
  32.  
    public void onClick(DialogInterface dialogInterface, int i) {
  33.  
     
  34.  
    }
  35.  
     
  36.  
    });
  37.  
    //3.创建对象,显示对象
  38.  
    builder.create().show();
学新通

学新通

学新通

1.5setAdapter设置自定义的样式

  • 需要传入一个自定义的布局

1)子布局样式

  • 文本框
  • 输入框
  • 多选框
  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:orientation="vertical"
  4.  
    android:layout_width="match_parent"
  5.  
    android:layout_height="match_parent">
  6.  
     
  7.  
    <LinearLayout
  8.  
    android:layout_width="match_parent"
  9.  
    android:layout_height="wrap_content"
  10.  
    android:orientation="horizontal"
  11.  
    >
  12.  
    <TextView
  13.  
    android:id="@ id/s1"
  14.  
    android:layout_width="wrap_content"
  15.  
    android:layout_height="wrap_content"
  16.  
    android:text=""
  17.  
    />
  18.  
    <EditText
  19.  
    android:layout_width="0dp"
  20.  
    android:layout_height="wrap_content"
  21.  
    android:id="@ id/s2"
  22.  
    android:layout_weight="1"
  23.  
    />
  24.  
    <CheckBox
  25.  
    android:layout_width="wrap_content"
  26.  
    android:layout_height="wrap_content"
  27.  
    android:id="@ id/s3"
  28.  
    />
  29.  
    </LinearLayout>
  30.  
    </LinearLayout>
学新通

2)Java代码

  1.  
    // 设置多选
  2.  
    String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
  3.  
    //1.创建构造器
  4.  
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
  5.  
    //2.设置参数
  6.  
    builder.setTitle("请选择城市地区")
  7.  
    .setIcon(R.mipmap.city)
  8.  
    .setAdapter(new ArrayAdapter<String>(DialogTest.this, R.layout.myselect,R.id.s1,citys), new DialogInterface.OnClickListener() {
  9.  
    @Override
  10.  
    public void onClick(DialogInterface dialogInterface, int i) {
  11.  
     
  12.  
    }
  13.  
    }
  14.  
    )
  15.  
    .setPositiveButton("确认",new DialogInterface.OnClickListener(){
  16.  
     
  17.  
    @Override
  18.  
    public void onClick(DialogInterface dialogInterface, int i) {
  19.  
     
  20.  
    }
  21.  
    })
  22.  
    .setNegativeButton("取消",new DialogInterface.OnClickListener(){
  23.  
     
  24.  
     
  25.  
    @Override
  26.  
    public void onClick(DialogInterface dialogInterface, int i) {
  27.  
     
  28.  
    }
  29.  
    })
  30.  
    .setNeutralButton("忽略",new DialogInterface.OnClickListener(){
  31.  
     
  32.  
    @Override
  33.  
    public void onClick(DialogInterface dialogInterface, int i) {
  34.  
     
  35.  
    }
  36.  
     
  37.  
    });
  38.  
    //3.创建对象,显示对象
  39.  
    builder.create().show();
学新通

学新通

1.6setView,指定对话框为自定义的View

1)布局代码

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:orientation="vertical"
  4.  
    android:layout_width="match_parent"
  5.  
    android:layout_height="match_parent">
  6.  
     
  7.  
    <LinearLayout
  8.  
    android:layout_width="match_parent"
  9.  
    android:layout_height="wrap_content"
  10.  
    android:orientation="horizontal"
  11.  
    >
  12.  
    <TextView
  13.  
    android:layout_width="wrap_content"
  14.  
    android:layout_height="wrap_content"
  15.  
    android:text="账号:"
  16.  
    android:textSize="30dp"
  17.  
    />
  18.  
    <EditText
  19.  
     
  20.  
    android:id="@ id/account"
  21.  
    android:layout_width="0dp"
  22.  
    android:layout_height="wrap_content"
  23.  
    android:hint="输入账号"
  24.  
    android:singleLine="true"
  25.  
    android:maxLength="16"
  26.  
    android:layout_weight="1"
  27.  
    android:textSize="30dp"
  28.  
    />
  29.  
     
  30.  
    </LinearLayout>
  31.  
    <LinearLayout
  32.  
    android:layout_width="match_parent"
  33.  
    android:layout_height="wrap_content"
  34.  
    android:orientation="horizontal"
  35.  
    >
  36.  
    <TextView
  37.  
    android:layout_width="wrap_content"
  38.  
    android:layout_height="wrap_content"
  39.  
    android:text="密码:"
  40.  
    android:textSize="30dp"
  41.  
    android:inputType="textPassword"
  42.  
    />
  43.  
    <EditText
  44.  
    android:id="@ id/password"
  45.  
    android:layout_width="0dp"
  46.  
    android:layout_height="wrap_content"
  47.  
    android:hint="输入密码"
  48.  
    android:singleLine="true"
  49.  
    android:maxLength="16"
  50.  
    android:layout_weight="1"
  51.  
    android:textSize="30dp"
  52.  
    />
  53.  
     
  54.  
    </LinearLayout>
  55.  
    <LinearLayout
  56.  
    android:layout_width="match_parent"
  57.  
    android:layout_height="wrap_content"
  58.  
    android:orientation="horizontal"
  59.  
    >
  60.  
     
  61.  
    <Button
  62.  
    android:id="@ id/lbtn1"
  63.  
    android:layout_width="0dp"
  64.  
    android:layout_weight="1"
  65.  
    android:layout_height="wrap_content"
  66.  
    android:text="登录"
  67.  
    android:textSize="30dp"
  68.  
    />
  69.  
    <Button
  70.  
    android:id="@ id/lbtn2"
  71.  
    android:layout_width="0dp"
  72.  
    android:layout_weight="1"
  73.  
    android:layout_height="wrap_content"
  74.  
    android:text="取消"
  75.  
    android:textSize="30dp"
  76.  
    />
  77.  
     
  78.  
    </LinearLayout>
  79.  
    </LinearLayout>
学新通

学新通

2)Java代码

  • dismiss,可以设置消失
  1.  
    // 6.自定义View
  2.  
    //1.获取布局
  3.  
    View view= LayoutInflater.from(this).inflate(R.layout.login,null);
  4.  
    //2.获取布局中的控件
  5.  
    EditText account=view.findViewById(R.id.account);
  6.  
    EditText password=view.findViewById(R.id.password);
  7.  
    Button lbtn1=view.findViewById(R.id.lbtn1);
  8.  
    Button lbtn2=view.findViewById(R.id.lbtn2);
  9.  
     
  10.  
    //3.创建构造器
  11.  
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
  12.  
    //4.设置参数
  13.  
    builder.setTitle("输入指定的登录信息")
  14.  
    .setIcon(R.mipmap.city)
  15.  
    .setView(view)
  16.  
    .setPositiveButton("确认",new DialogInterface.OnClickListener(){
  17.  
     
  18.  
    @Override
  19.  
    public void onClick(DialogInterface dialogInterface, int i) {
  20.  
     
  21.  
    }
  22.  
    })
  23.  
    .setNegativeButton("取消",new DialogInterface.OnClickListener(){
  24.  
     
  25.  
     
  26.  
    @Override
  27.  
    public void onClick(DialogInterface dialogInterface, int i) {
  28.  
     
  29.  
    }
  30.  
    })
  31.  
    .setNeutralButton("忽略",new DialogInterface.OnClickListener(){
  32.  
     
  33.  
    @Override
  34.  
    public void onClick(DialogInterface dialogInterface, int i) {
  35.  
     
  36.  
    }
  37.  
     
  38.  
    });
  39.  
    //5.创建对象,
  40.  
    AlertDialog alertDialog=builder.create();
  41.  
    //6.单独设置事件监听器
  42.  
    lbtn1.setOnClickListener(new View.OnClickListener() {
  43.  
    @Override
  44.  
    public void onClick(View view) {
  45.  
    if (account.getText().toString().equals("1001")&&password.getText().toString().equals("123456")){
  46.  
    Toast.makeText(DialogTest.this, "登录成功!", Toast.LENGTH_SHORT).show();
  47.  
    //===设置对话框消失===
  48.  
    alertDialog.dismiss();
  49.  
    }
  50.  
    }
  51.  
    });
  52.  
     
  53.  
    lbtn2.setOnClickListener(new View.OnClickListener() {
  54.  
    @Override
  55.  
    public void onClick(View view) {
  56.  
    Toast.makeText(DialogTest.this, "取消登录!", Toast.LENGTH_SHORT).show();
  57.  
    //===设置对话框消失===
  58.  
    alertDialog.dismiss();
  59.  
    }
  60.  
    });
  61.  
    //7.显示对象
  62.  
    alertDialog.show();
  63.  
    }
学新通

学新通

学新通

3)改建

  1.  
    public void loginAlert(View view1) {
  2.  
    // 6.自定义View
  3.  
    //1.获取布局
  4.  
    View view= LayoutInflater.from(this).inflate(R.layout.login,null);
  5.  
    //2.获取布局中的控件
  6.  
    EditText account=view.findViewById(R.id.account);
  7.  
    EditText password=view.findViewById(R.id.password);
  8.  
    Button lbtn1=view.findViewById(R.id.lbtn1);
  9.  
    Button lbtn2=view.findViewById(R.id.lbtn2);
  10.  
     
  11.  
    //3.创建构造器
  12.  
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
  13.  
    //4.设置参数
  14.  
    builder.setTitle("输入指定的登录信息")
  15.  
    .setIcon(R.mipmap.city)
  16.  
    .setView(view)
  17.  
    .setPositiveButton("确认",new DialogInterface.OnClickListener(){
  18.  
     
  19.  
    @Override
  20.  
    public void onClick(DialogInterface dialogInterface, int i) {
  21.  
     
  22.  
    }
  23.  
    })
  24.  
    .setNegativeButton("取消",new DialogInterface.OnClickListener(){
  25.  
     
  26.  
     
  27.  
    @Override
  28.  
    public void onClick(DialogInterface dialogInterface, int i) {
  29.  
     
  30.  
    }
  31.  
    })
  32.  
    .setNeutralButton("忽略",new DialogInterface.OnClickListener(){
  33.  
     
  34.  
    @Override
  35.  
    public void onClick(DialogInterface dialogInterface, int i) {
  36.  
     
  37.  
    }
  38.  
     
  39.  
    });
  40.  
    //5.创建对象,
  41.  
    AlertDialog alertDialog=builder.create();
  42.  
    //6.单独设置事件监听器
  43.  
    lbtn1.setOnClickListener(new View.OnClickListener() {
  44.  
    @Override
  45.  
    public void onClick(View view) {
  46.  
    if (account.getText().toString().equals("1001")&&password.getText().toString().equals("123456")){
  47.  
    Toast.makeText(DialogTest.this, "登录成功!", Toast.LENGTH_SHORT).show();
  48.  
    //===设置对话框消失===
  49.  
    alertDialog.dismiss();
  50.  
    }
  51.  
    }
  52.  
    });
  53.  
     
  54.  
    lbtn2.setOnClickListener(new View.OnClickListener() {
  55.  
    @Override
  56.  
    public void onClick(View view) {
  57.  
    Toast.makeText(DialogTest.this, "取消登录!", Toast.LENGTH_SHORT).show();
  58.  
    //===设置对话框消失===
  59.  
    alertDialog.dismiss();
  60.  
    }
  61.  
    });
  62.  
    //7.显示对象
  63.  
    alertDialog.show();
  64.  
    }
学新通

学新通

点击弹出框之后的

学新通

学新通

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcakke
系列文章
更多 icon
同类精品
更多 icon
继续加载