关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » 安卓开发 »Android UI控件系列:Dialog(对话框)

    Android UI控件系列:Dialog(对话框)

    2014-12-03 00:00:00 出处:richie.wang的博客
    分享

    对话框是Android中不可或缺的,在使用对话框的时候,需要使用AlertDialog.Builder类。当然处理系统默认的对话框外,还可以自定义对话框,假如对话框设置了按钮,那么要对其进行事件监听OnClickListener。

    下面的是一个用AlertDialog.Builder类和自定义的对话框的实例,当点击确定时,转移到登陆对话框,当输入用户名和密码后,转移到登陆进度对话框

    这里的自定义对话框是由两个Textview和两个EditText组成,也就是那个登录对话框,自定义对话框的布局文件是dialog.xml文件,见下面

    另外呢,使用AlertDialog来创建对话框,需要了解一下几个方法

    setTitle();给对话框设置标题
    setIcon();给对话框设置图标
    setMessage();设置对话框的提示信息
    setItems();设置对话框要显示的一个list,一般用于显示几个命令时
    setSingleChoiceItems();设置对话框显示一个单选的List
    setMultiChoiceItems();设置对话框显示一系列的复选框
    setPositiveButton();给对话框添加”yes”按钮
    setNegativeButton();给对话框添加”no”按钮

    DialogTest.java

    package org.hualang.dialog;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    
    public class DialogTest extends Activity {
        /** Called when the activity is first created. */
            ProgressDialog mydialog;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Dialog dialog=new AlertDialog.Builder(DialogTest.this)
            .setTitle("登录提示")//设置标题
            .setMessage("这里需要登录")//设置对话框显示内容
            .setPositiveButton("确定", //设置确定按钮
            new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                    //点击确定转向登录对话框
                                    LayoutInflater factory=LayoutInflater.from(DialogTest.this);
                                    //得到自定义对话框
                                    final View DialogView=factory.inflate(R.layout.dialog, null);
                                    //创建对话框
                                    AlertDialog dlg=new AlertDialog.Builder(DialogTest.this)
                                    .setTitle("登录框")
                                    .setView(DialogView)//设置自定义对话框样式
                                    .setPositiveButton("确定",
                                    new DialogInterface.OnClickListener() {//设置监听事件
    
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                    // 输入完成后点击“确定”开始登录
                                                    mydialog=ProgressDialog.show(DialogTest.this, "请稍等...", "正在登录...",true);
                                                    new Thread()
                                                    {
                                                            public void run()
                                                            {
                                                                    try
                                                                    {
                                                                            sleep(3000);
                                                                    }catch(Exception e)
                                                                    {
                                                                            e.printStackTrace();
                                                                    }finally
                                                                    {
                                                                            //登录结束,取消mydialog对话框
                                                                            mydialog.dismiss();
                                                                    }
                                                            }
                                                    }.start();
                                            }
                                    }).setNegativeButton("取消",//设置取消按钮
                                            new DialogInterface.OnClickListener() {
    
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                            //点击取消后退出程序
                                                            DialogTest.this.finish();
    
                                                    }
                                            }).create();//创建对话框
                                    dlg.show();//显示对话框
                            }
                    }).setNeutralButton("退出",
                            new DialogInterface.OnClickListener() {
    
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                            // 点击退出后退出程序
                                            DialogTest.this.finish();
                                    }
                            }).create();//创建按钮
            //显示对话框
            dialog.show();
        }
    }

    dialog.xml

    < xml version="1.0" encoding="utf-8" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView
            android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="用户名"
        android:gravity="left"
        android:textAppearance=" android:attr/textAppearanceMedium"
        />
    <EditText
            android:id="@+id/username"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:scrollHorizontally="true"
            android:autoText="false"
            android:capitalize="none"
            android:gravity="fill_horizontal"
            android:textAppearance=" android:attr/textAppearanceMedium"
    />
    <TextView
            android:id="@+id/password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:text="密码"
            android:gravity="left"
            android:textAppearance=" android:attr/textAppearanceMedium"
    />
    <EditText
            android:id="@+id/password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:scrollHorizontally="true"
            android:autoText="false"
            android:capitalize="none"
            android:gravity="fill_horizontal"
            android:password="true"
            android:textAppearance=" android:attr/textAppearanceMedium"
    
    />
    </LinearLayout>

    运行结果如下:

    点击确定后,会跳转到登陆对话框,这个登录对话框是自定义的对话框

    输入用户名和密码后,点击确定后,进入带进度条的对话框中,这里的带进度条的对话框是系统默认的,并且用现成控制,3秒后就结束该对话框,并跳转到相应的Activity中

    补充内容:

    1、Inflater英文意思是膨胀,在android中是扩展的意思。

    LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。 它可以有很多地方可以使用,如BaseAdapter的getView中,自定义Dialog中取得view中的组件widget等等。

    2、AlertDialog.Builder是警告对话框的意思

    3、单位

    px:是屏幕的像素点
    in:英寸
    mm:毫米
    pt:磅,1/72 英寸
    dp:一个基于density的抽象单位,假如一个160dpi的屏幕,1dp=1px
    dip:等同于dp
    sp:同dp相似,但还会根据用户的字体大小偏好来缩放。

    建议使用sp作为文本的单位,其它用dip

    4、dialog.xml说明

    ①android:layout_marginLeft=”20dip”
    margin是边的意思,上面这句话是说该控件距离左边20个dip。同样
    android:layout_marginRight=”20dip”就是该控件距离父控件右边20dip

    ②android:gravity=”left”:表示该控件的text文本显示在左边

    ③android:layout_gravity=”center”:表示该控件位于父控件的中间

    ④android:textAppearance的使用

    对于能够显示文字的控件(如TextView EditText RadioButton Button CheckBox等),你有时需要控制字体的大小。Android平台定义了三种字体大小。

    “ android:attr/textAppearanceLarge”
    “ android:attr/textAppearanceMedium”
    “ android:attr/textAppearanceSmall”
    使用方法为:
    android:textAppearance=” android:attr/textAppearanceLarge”
    android:textAppearance=” android:attr/textAppearanceMedium”
    android:textAppearance=” android:attr/textAppearanceSmall”
    或
    style=” android:attr/textAppearanceLarge”
    style=” android:attr/textAppearanceMedium”
    style=” android:attr/textAppearanceSmall”

    ⑤ android:scrollHorizontally=”true”:设置文本超出TextView的宽度的情况下,是否出现横拉条

    ⑥android:autoText=”false”:假如设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。此处设置为false,则为关闭子动能

    ⑦android:capitalize=”none”:设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到

    ⑧android:password=”true”:以小点”.”显示文本,用于输入密码时

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    电脑屏幕不小心竖起来了?别慌,快捷键搞定

    Destoon 模板存放规则及语法参考

    Destoon系统常量与变量

    Destoon系统目录文件结构说明

    Destoon 系统安装指南

    Destoon会员公司主页模板风格添加方法

    Destoon 二次开发入门

    Microsoft 将于 2026 年 10 月终止对 Windows 11 SE 的支持

    Windows 11 存储感知如何设置?了解Windows 11 存储感知开启的好处

    Windows 11 24H2 更新灾难:系统升级了,SSD固态盘不见了...

    IT头条

    Synology 更新 ActiveProtect Manager 1.1 以增强企业网络弹性和合规性

    00:43

    新的 Rubrik Agent Cloud 加速了可信的企业 AI 代理部署

    00:34

    宇树科技 G1人形机器人,拉动一辆重达1.4吨的汽车

    00:21

    Cloudera 调查发现,96% 的企业已将 AI 集成到核心业务流程中,这表明 AI 已从竞争优势转变为强制性实践

    02:05

    投资者反对马斯克 1 万亿美元薪酬方案,要求重组特斯拉董事会

    01:18

    技术热点

    大型网站的 HTTPS 实践(三):基于协议和配置的优化

    ubuntu下右键菜单添加新建word、excel文档等快捷方式

    Sublime Text 简明教程

    用户定义SQL Server函数的描述

    怎么在windows 7开始菜单中添加下载选项?

    SQL Server 2016将有哪些功能改进?

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。