前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >对话框(AlertDialog)使用

对话框(AlertDialog)使用

作者头像
李小白是一只喵
发布2020-04-24 08:34:21
1.7K0
发布2020-04-24 08:34:21
举报

image.png

目录

AlertDialog

AlertDialog也就处对话框。 使用方式分为6种:

  1. 简单dialog
  2. 列表dialog
  3. 单选dialog
  4. 多选dialog
  5. 自定义dialog
  6. 使用adapter的dialog

举例第一种的使用 代码:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher_round);

        builder.setTitle("桃子");
        builder.setMessage("喜欢吃桃子吗?");

        builder.setPositiveButton("yes",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "想吃", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("不喜欢吃",null);
        builder.setNeutralButton("不知道",null);

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

实战

activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="简单dialog"
            android:onClick="dialog_1"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="列表dialog"
            android:onClick="dialog_2"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="单选的dialog"
            android:onClick="dialog_3"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="多选dialog"
            android:onClick="dialog_4"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="自定义dialog"
            android:onClick="dialog_5"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="使用adapter的dialog"
            android:onClick="dialog_6"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

代码:

package com.example.user.alertdiog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    int index;
    private String [] fru = {"苹果","桃子","西红柿","芒果","数学老师"};
    boolean[] flag = {false,false,false,false,false};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void dialog_1(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher_round);

        builder.setTitle("桃子");
        builder.setMessage("喜欢吃桃子吗?");

        builder.setPositiveButton("yes",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "想吃", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("不喜欢吃",null);
        builder.setNeutralButton("不知道",null);

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void dialog_2(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择");
        builder.setItems(fru, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "选择了"+fru[which], Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void dialog_3(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择");
        builder.setSingleChoiceItems(fru, index, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                index = which;
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "选择了"+fru[index], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void dialog_4(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择");
        builder.setMultiChoiceItems(fru, flag, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                flag[which] = isChecked;
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuffer str = new StringBuffer();
                for (int i = 0; i < fru.length; i++) {
                    if (flag[i]) {
                        str.append(fru[i] + " ");
                    }
                }
                Toast.makeText(MainActivity.this, "选择了" + str.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void dialog_5(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("爱吃的水果");
        final EditText et = new EditText(this);
        et.setHint("哦我爱吃的水果是什么");
        et.setSingleLine(true);
        builder.setView(et);
        builder.setNegativeButton("取消",null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String password = et.getText().toString();
                if (password.equals("桃子")) {
                    Toast.makeText(MainActivity.this, "正确", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void dialog_6(View v){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fru);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("水果超市");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "选择了"+fru[which], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}

运行效果:

image.png

简单的对话框:

image.png

列表对话框:

image.png

单选对话框:

image.png

多选对话框:

image.png

自定义对话框:

image.png

使用adapter的对话框:

image.png

参考

AlertDialog的几种用法

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • AlertDialog
  • 实战
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档