Android UI 之 Dialog

本文介绍了Android中的对话框组件,包括AlertDialog的基本使用方法及其常用配置选项,并提供了一个自定义LoginDialog的示例,展示了如何创建一个包含自定义视图和监听器的对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dialog控件就是一个对话框,显示在屏幕中间的小窗体,其宽高由其内容决定,所有的对话框类都直接或间接继承Dialog类。

一、AlertDialog
AlertDialog直接继承于Dialog类,通过使用Builder类来构建AlertDialog
下面是一个简单的AlertDialog的实现:

public void titleDialog(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Info Title");
        builder.setMessage(R.string.info);
        builder.create().show();
}

先创建builder对象,然后使用setTitle方法设置对话框的标题,setMessage方法设置对话框的内容,最后通过create方法构建AlertDialog对象,如果要显示出对话框就调用show方法。
AlertDialog除了上面的方法还有以下这些常用的方法:
设置按钮的方法:
setPositiveButton():一个积极的按钮,一般用于“OK”或者“继续”等操作。
setNegativeButton():一个负面的按钮,一般用于“取消”操作。
setNeutralButton():一个比较中性的按钮,一般用于“忽略”、“以后提醒我”等操作。
AlertDialog本身方法:
dialog.cancle() 取消窗口,会执行DialogInterface.OnCancelListener
dialog.dismiss() 取消窗口,会执行DialogInterface.OnDismissListener
以上两种方法都是关闭对话框,只是调用的监听器不同
设置列表选项方法:
setItems,设置普通的列表
setSingleChoiceItems 设置带有单选按钮的列表
setMultiChoiceItems 设置带有多选按钮的列表
其他方法:
setIcon 设置对话框的图标
setCancelable设置点击对话框以外的地方是否可以关闭对话框,true表示可以

二、自定义Dialog
自定义Dialog有很多方法,最简单的就是创建一个类继承AlertDialog类
下面是我个人自定义的LoginDialog

package com.example.yougel.customdialogdemo;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;


public class LoginDialog extends AlertDialog {

    public  Context context;
    boolean isshow;
    //构造方法
    protected LoginDialog(Context context) {
        super(context);
        this.context=context;
    }

    //设置是否显示标题
    public void setShowTitle(boolean isshow){
        this.isshow=isshow;
    }

    //登录监听接口
    public interface onLoginListener {
        void onLogin(View v);
    }
    //监听器对象
    onLoginListener monLoginListener;

    //设置登录监听器
    public void setOnLoginListener(onLoginListener onLoginListener ){
        monLoginListener=onLoginListener;
    }

    //显示LoginDialog
    public void show(){
        View view=View.inflate(context,R.layout.login_dialog,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setView(view);
        if(isshow){
            TextView title= (TextView) view.findViewById(R.id.login_title);
            title.setText("用户登录");
        }
        final CheckBox agree= (CheckBox) view.findViewById(R.id.agree);
        final Button login= (Button) view.findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(agree.isChecked()){
                    monLoginListener.onLogin(login);
                }else{
                    Toast.makeText(context,"请选择已经阅读用户协议",Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.create().show();
    }
}

在Activity中的调用:

public void createLoginDialog(){
        LoginDialog loginDialog=new LoginDialog(getContext());
        loginDialog.setShowTitle(false);
        loginDialog.setOnLoginListener(new LoginDialog.onLoginListener() {
            @Override
            public void onLogin(View v) {
                Toast.makeText(getContext(),"登录",Toast.LENGTH_SHORT).show();
            }
        });
        loginDialog.show();
    }

我在自定义的Dialog类中添加了一个自定义的监听器
(1)创建一个自定义监听接口;
(2)定义一个自定义监听接口对象;
(3)定义一个设置监听接口的方法;
(4)在自定义的Dialog中使用监听接口对象调用接口的方法。(我这里就是点击按钮时调用)
以下是LoginDialog的布局文件:

<?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"
    >
    <TextView
        android:gravity="center"
        android:id="@+id/login_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/font_color"
        android:textSize="26sp"
        android:background="@drawable/login_title_style"
        />
    <EditText
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:textColorHint="@color/hint"
        android:id="@+id/account"
        />
    <EditText
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:id="@+id/password"
        android:textColorHint="@color/hint"
        android:hint="密码"/>
    <CheckBox
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/agree"
        android:text="已经阅读用户协议"
        />
    <Button
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:textColor="@color/font_color"
        android:id="@+id/login"
        android:background="@drawable/btn_style"
        android:text="登录"/>
</LinearLayout>

最后的界面:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值