Android自定义Dialog
Android开发过程中,常常会遇到一些需求场景——在界面上弹出一个弹框,对用户进行提醒并让用户进行某些选择性的操作,
如退出登录时的弹窗,让用户选择“退出”还是“取消”等操作。
Android系统提供了Dialog类,以及Dialog的子类,常见如AlertDialog来实现此类功能。
一般情况下,利用Android提供的Dialog及其子类能够满足多数此类需求,然而,其不足之处体现在:
1. 基于Android提供的Dialog及其子类样式单一,风格上与App本身风格可能不太协调;
2. Dialog弹窗在布局和功能上有所限制,有时不一定能满足实际的业务需求。
本文将通过在Dialog基础上构建自定义的Dialog弹窗,以最常见的确认弹框为例。
本样式相对比较简单:上面有一个弹框标题(提示语),下面左右分别是“确认”和“取消”按钮,当用户点击“确认”按钮时,弹框执行
相应的确认逻辑,当点击“取消”按钮时,执行相应的取消逻辑。
首先,自定义弹框样式:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="wrap_content"
5 android:background="@drawable/dialog_bg"
6 android:orientation="vertical" >
7
8 <TextView
9 android:id="@+id/title"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:layout_gravity="center"
13 android:paddingTop="14dp"
14 android:textColor="@color/login_hint"
15 android:textSize="@dimen/text_size_18" />
16
17 <LinearLayout
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:layout_marginBottom="14dp"
21 android:layout_marginLeft="20dp"
22 android:layout_marginRight="20dp"
23 android:layout_marginTop="30dp" >
24
25 <TextView
26 android:id="@+id/confirm"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:layout_marginRight="10dp"
30 android:layout_weight="1"
31 android:background="@drawable/btn_confirm_selector"
32 android:gravity="center"
33 android:textColor="@color/white"
34 android:textSize="@dimen/text_size_16" />
35
36 <TextView
37 android:id="@+id/cancel"
38 android:layout_width="wrap_content"
39 android:layout_height="wrap_content"
40 android:layout_marginLeft="10dp"
41 android:layout_weight="1"
42 android:background="@drawable/btn_cancel_selector"
43 android:gravity="center"
44 android:textColor="@color/login_hint"
45 android:textSize="@dimen/text_size_16" />
46 </LinearLayout>
47
48 </LinearLayout>

然后,通过继承Dialog类构建确认弹框控件ConfirmDialog:

1 package com.corn.widget;
2
3 import android.app.Dialog;
4 import android.content.Context;
5 import android.os.Bundle;
6 import android.util.DisplayMetrics;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.Window;
10 import android.view.WindowManager;
11 import android.widget.TextView;
12
13 import com.corn.R;
14
15 public class ConfirmDialog extends Dialog {
16
17 private Context context;
18 private String title;
19 private String confirmButtonText;
20 private String cacelButtonText;
21 private ClickListenerInterface clickListenerInterface;
22
23 public interface ClickListenerInterface {
24
25 public void doConfirm();
26
27 public void doCancel();
28 }
29
30 public ConfirmDialog(Context context, String title, String confirmButtonText, String cacelButtonText) {
31 super(context, R.style.MyDialog);
32 this.context = context;
33 this.title = title;
34 this.confirmButtonText = confirmButtonText;
35 this.cacelButtonText = cacelButtonText;
36 }
37
38 @Override
39 protected void onCreate(Bundle savedInstanceState) {
40 // TODO Auto-generated method stub
41 super.onCreate(savedInstanceState);
42
43 init();
44 }
45
46 public void init() {
47 LayoutInflater inflater = LayoutInflater.from(context);
48 View view = inflater.inflate(R.layout.confirm_dialog, null);
49 setContentView(view);
50
51 TextView tvTitle = (TextView) view.findViewById(R.id.title);
52 TextView tvConfirm = (TextView) view.findViewById(R.id.confirm);
53 TextView tvCancel = (TextView) view.findViewById(R.id.cancel);
54
55 tvTitle.setText(title);
56 tvConfirm.setText(confirmButtonText);
57 tvCancel.setText(cacelButtonText);
58
59 tvConfirm.setOnClickListener(new clickListener());
60 tvCancel.setOnClickListener(new clickListener());
61
62 Window dialogWindow = getWindow();
63 WindowManager.LayoutParams lp = dialogWindow.getAttributes();
64 DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
65 lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
66 dialogWindow.setAttributes(lp);
67 }
68
69 public void setClicklistener(ClickListenerInterface clickListenerInterface) {
70 this.clickListenerInterface = clickListenerInterface;
71 }
72
73 private class clickListener implements View.OnClickListener {
74 @Override
75 public void onClick(View v) {
76 // TODO Auto-generated method stub
77 int id = v.getId();
78 switch (id) {
79 case R.id.confirm:
80 clickListenerInterface.doConfirm();
81 break;
82 case R.id.cancel:
83 clickListenerInterface.doCancel();
84 break;
85 }
86 }
87
88 };
89
90 }

在如上空间构造代码中,由于控件的"确认"和"取消"逻辑与实际的应用场景有关,因此,控件中通过定义内部接口来实现。
在需要使用此控件的地方,进行如下形式调用:

1 public static void Exit(final Context context) {
2 final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定要退出吗?", "退出", "取消");
3 confirmDialog.show();
4 confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
5 @Override
6 public void doConfirm() {
7 // TODO Auto-generated method stub
8 confirmDialog.dismiss();
9 //toUserHome(context);
10 AppManager.getAppManager().AppExit(context);
11 }
12
13 @Override
14 public void doCancel() {
15 // TODO Auto-generated method stub
16 confirmDialog.dismiss();
17 }
18 });
19 }

调用中实现了此控件的内部接口,并赋给控件本身,以此在点击按钮时实现基于外部具体业务逻辑的函数回调。
Android自定义Dialog的更多相关文章
- Android自定义 Dialog 对话框
Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...
- Android—自定义Dialog
在 Android 日常的开发中,Dialog 使用是比较广泛的.无论是提示一个提示语,还是确认信息,还是有一定交互的(弹出验证码,输入账号密码登录等等)对话框. 而我们去看一下原生的对话框,虽然随着 ...
- Android自定义Dialog(美化界面)
前言:在做项目的时候,发现dialog界面太丑陋,从csdn上下载了一份自定义dialog的源码,在他的基础上对界面进行美化...有需要的朋友可以直接拿走 效果图如下: 主要代码: /** * 自定义 ...
- Android自定义Dialog及其布局
实际项目开发中默认的Dialog样式无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件. 上效果图: 自定义Dialog,LogoutDialog: 要将自定义布局传入构造函数中, ...
- android 自定义Dialog背景透明及显示位置设置
先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...
- android 自定义Dialog去除黑色边框
在自定义Dialog时显示的界面中老是有黑色的边框,下面就介绍使用style去除黑色边框方法. 首先在values/styles定义自定义样式: <style name="MyDial ...
- Android 自定义Dialog类,并在Activity中实现按钮监听。
实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的 ...
- Android 自定义Dialog 去除阴影
自定义Dialog中添加下列代码: window.clearFlags( WindowManager.LayoutParams.FLAG_DIM_BEHIND);
- android自定义dialog布局
dialog使用系统自带的有时候不是很美观,就想要自己来设计一个dialog界面,以下就是可以设计的dialog界面: public class CustomDialog extends Dialog ...
随机推荐
- DELETE和DELETE FROM有什么区别
你如果只针对表进行删除,则一样. 但是如果需要联合其他表,则需要使用from 例如 delete tb1 from tb1 m where id in (select id from tb2) 我的习 ...
- 【POJ 1062】昂贵的聘礼(最短路)
Dijkstra最短路,每次限制一个等级差,再更新答案. #include <cstdio> #define N 105 #define INF 1e9 int m, n; int p[N ...
- CSS3系列二(媒体支持、文字与字体相关样式、盒相关样式)
CSS3媒体支持 在css3中允许我们在不改变内容的情况下,在样式中选择一种页面的布局以精确的适应不同的设备,从而改善用户体验 可以利用meta标签在页面中指定浏览器在处理本页面时按照多少像素的窗口宽 ...
- Chrome浏览器插件
Chrome 布局 1. 修改Chrome Dock side Chrome 更多工具 -> 开发者工具 -> Customsize and Control Dev Tools
- FluentData,它是一个轻量级框架,关注性能和易用性。
http://www.cnblogs.com/zengxiangzhan/p/3250105.html FluentData,它是一个轻量级框架,关注性能和易用性. 下载地址:FlunenData.M ...
- Putty远程登录VMware虚拟机Linux(Ubuntu)
安装SSH服务 Ubuntu默认并没有安装ssh服务,如果通过ssh链接ubuntu,需要自己手动安装ssh-server.判断是否安装ssh服务,可以通过如下命令进行: www.linuxidc.c ...
- Android 实现卫星菜单
步骤:一:自定义ViewGroup 1.自定义属性 a.attr.xml b.在布局文件中使用activity_main.xml c.在自定义控件中进行读取 2.onMeasure 3.onLayou ...
- 统计网站访问量,以GD2库图像形式输出
index.php页面<?php session_start(); if($_SESSION[temp]==""){ //判断$_SESSION[temp]=="& ...
- Servlet之Filter详细讲解
Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时候,有时候发一些敏感的信息,发出后显示时 就会将敏感信息用*等字符替代,这就是用过滤器对信息进行 ...
- 织梦DedeCMS首页调用单页文档内容的方法
很多使用织梦dedecms单页文档功能的朋友都想知道如何在织梦首页调用单页文档的内容,下面就教大家具体的实现方法: 具体步骤如下: 首先在首页模板需要显示单页文档内容的地方插入如下代码: {dede: ...