关闭 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”:以小点”.”显示文本,用于输入密码时

    上一篇返回首页 下一篇

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

    别人在看

    正版 Windows 11产品密钥怎么查找/查看?

    还有3个月,微软将停止 Windows 10 的更新

    Windows 10 终止支持后,企业为何要立即升级?

    Windows 10 将于 2025年10 月终止技术支持,建议迁移到 Windows 11

    Windows 12 发布推迟,微软正全力筹备Windows 11 25H2更新

    Linux 退出 mail的命令是什么

    Linux 提醒 No space left on device,但我的空间看起来还有不少空余呢

    hiberfil.sys文件可以删除吗?了解该文件并手把手教你删除C盘的hiberfil.sys文件

    Window 10和 Windows 11哪个好?答案是:看你自己的需求

    盗版软件成公司里的“隐形炸弹”?老板们的“法务噩梦” 有救了!

    IT头条

    公安部:我国在售汽车搭载的“智驾”系统都不具备“自动驾驶”功能

    02:03

    液冷服务器概念股走强,博汇、润泽等液冷概念股票大涨

    01:17

    亚太地区的 AI 驱动型医疗保健:2025 年及以后的下一步是什么?

    16:30

    智能手机市场风云:iPhone领跑销量榜,华为缺席引争议

    15:43

    大数据算法和“老师傅”经验叠加 智慧化收储粮食尽显“科技范”

    15:17

    技术热点

    SQL汉字转换为拼音的函数

    windows 7系统无法运行Photoshop CS3的解决方法

    巧用MySQL加密函数对Web网站敏感数据进行保护

    MySQL基础知识简介

    Windows7和WinXP下如何实现不输密码自动登录系统的设置方法介绍

    windows 7系统ip地址冲突怎么办?windows 7系统IP地址冲突问题的

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

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