Android之AlertDialog对话框

本文详细介绍了Android中AlertDialog的使用,包括默认样式、单选和多选样式,以及如何自定义对话框样式,展示了从样式一到样式五的各种定制案例。
  • 默认样式
  • 单选样式
  • 多选样式
  • 自定义样式

自定义的登陆的layout_login

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    >
    <EditText
        android:id="@+id/edit_user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="userName"
        android:maxLines="1"
        />

    <EditText
        android:id="@+id/edit_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="password"
        android:inputType="textPassword"
        android:maxLines="1"
        />

    <Button
        android:id="@+id/btn_dialog_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆"/>
</LinearLayout>

package com.example.test0508;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test0508.util.ToastUtil;

public class DialogActivity extends AppCompatActivity {


    private Button mBtndialog1,mBtndialog2,mBtndialog3,mBtndialog4,mBtndialog5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        mBtndialog1 = findViewById(R.id.btn_dialog1);
        mBtndialog2 = findViewById(R.id.btn_dialog2);
        mBtndialog3 = findViewById(R.id.btn_dialog3);
        mBtndialog4 = findViewById(R.id.btn_dialog4);
        mBtndialog5 = findViewById(R.id.btn_dialog5);
        OnClick onClick = new OnClick();
        mBtndialog1.setOnClickListener(onClick);
        mBtndialog2.setOnClickListener(onClick);
        mBtndialog3.setOnClickListener(onClick);
        mBtndialog4.setOnClickListener(onClick);
        mBtndialog5.setOnClickListener(onClick);
    }

    class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_dialog1:
                    //弹出一个对话框
                    AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                    builder.setTitle("请回答").setMessage("你觉得课程如何").setIcon(R.drawable.xuegao)
                            .setPositiveButton("棒", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ToastUtil.showMessage(DialogActivity.this,"xiexie");
                                }
                            })
                            .setNeutralButton("还行", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ToastUtil.showMessage(DialogActivity.this,"你再看看");
                                }
                            })
                            .setNegativeButton("不好", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ToastUtil.showMessage(DialogActivity.this,"滚");
                                }
                            }).show();
                    break;
                /**
                 * *****************************************
                 * 样式二
                 */
                case R.id.btn_dialog2:
                    //单选
                    final String[] array = new String[]{"男","女"};
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
                    builder1.setTitle("性别").setItems(array, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMessage(DialogActivity.this,array[i]);
                        }
                    }).show();
                    break;

                /**
                 * *****************************************
                 * 样式3
                 */
                case R.id.btn_dialog3:
                    final String[] array3 = new String[]{"男","女"};
                    AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
                    builder3.setTitle("您的性别是")
                    .setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMessage(DialogActivity.this,array3[i]);
                            dialogInterface.dismiss();
                        }
                    }).setCancelable(false).show() ;
                    break;


                /**
                 * *****************************************
                 * 样式4 多选
                 */
                case R.id.btn_dialog4:
                    final String[] array4 = new String[]{"唱歌","跳舞","写代码"};
                    final boolean[] isSelected = new boolean[]{false,false,true};
                    AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
                    builder4.setTitle("你的兴趣是")
                            .setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                    ToastUtil.showMessage(DialogActivity.this,array4[i]+":"+isSelected[i]);
                                }
                            }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //随便写
                        }
                    }).setNegativeButton("取消按钮", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //
                        }
                    })
                            .show();
                    break;

                /**
                 * *****************************************
                 * 样式4 多选
                 */
                case R.id.btn_dialog5:
                    AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
                    View view1 =  LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
                    EditText userName = view1.findViewById(R.id.edit_user_name);
                    EditText password = view1.findViewById(R.id.edit_password);
                    Button button = view1.findViewById(R.id.btn_dialog_login);
                    //对这个登陆按钮设计点击事件
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //获取username和password
                        }
                    });
                    builder5.setTitle("登陆操作")
                            .setView(view1).show();
                    break;

            }
        }
    }
}

<?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:padding="10dp"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/btn_dialog1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="样式1"
        android:textAllCaps="false"

    />

    <Button
        android:id="@+id/btn_dialog2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="样式2"
        android:textAllCaps="false"

        />
    <Button
        android:id="@+id/btn_dialog3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="样式3"
        android:textAllCaps="false"

        />
    <Button
        android:id="@+id/btn_dialog4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="样式4"
        android:textAllCaps="false"

        />

    <Button
        android:id="@+id/btn_dialog5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="样式5"
        android:textAllCaps="false"

        />

</LinearLayout>

样式一
在这里插入图片描述
样式二
在这里插入图片描述
样式三
在这里插入图片描述
样式五
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值