Android中Dialog的使用
上一篇博文讲到对话框popWindow的使用,这篇博文主要解说Dialog的使用。
1、什么是Dialog?
Dialog就是对话框的一种方式!
在Android开发中。我们常常会须要在Android界面上弹出一些对话框,比方询问用户或者让用户选择。这样的对话框叫Dialog。最常常使用的,大家也比較熟悉的。也使用比較频繁有AlertDialog,这边篇博文将比較详尽的解说Dialog的使用。
2、Dialog的特性
Android的对话框有两种:PopupWindow和Dialog。它们的不同点在于:
Dialog的位置固定,而PopupWindow的位置能够任意。
Dialog是非堵塞线程的,而PopupWindow是堵塞线程的。
以上两点是PopupWindow和Dialog最大的不同。
3、AlertDialog的使用
通过查看这句代码AlertDialog定义的源代码
- public class android.app.AlertDialog extends android.app.Dialog implements android.content.DialogInterface
我们能够发现AlertDialog是继承于Dialog的。然而AlertDialog有几种使用方法呢。经过整理AlertDialog常常使用的有7种。详细请查看我的博文:
4、Diglog的重写
当AlertDialog不能满足我们的需求时。我们应该怎么做,由于AlertDialog是继承于Dialog的。那我们是不是也自己重写一个Dialog呢。这个是能够的
下面一个重写Dialog的小演示样例:
重写Dialog的界面:
XMl界面代码为:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#60000000"
- android:gravity="center"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:background="@drawable/basedl_bg"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="@dimen/dialog_face_hight"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:layout_marginTop="20dp"
- android:background="@drawable/fw_dialog_signtype_blue"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/basedl_iv_head"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="2.5"
- android:padding="5dp"
- android:src="@drawable/ic_head_default" />
- <LinearLayout
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="7.5"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1" >
- <TextView
- android:id="@+id/anyfish_dialog_tv_hint"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_alignParentLeft="true"
- android:gravity="bottom|left"
- android:paddingLeft="15dp"
- android:text="对话框名称"
- android:textColor="#ffffff" />
- </RelativeLayout>
- <TextView
- android:id="@+id/basedl_tv_hint02"
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:gravity="center_vertical|left"
- android:paddingLeft="15dp"
- android:text="对话框凝视。解释这个对话框作用"
- android:textColor="#ffffff"
- android:textSize="@dimen/dialog_fistype_exp_textsize" />
- </LinearLayout>
- </LinearLayout>
- <EditText
- android:id="@+id/anyfish_dialog_et_input"
- android:layout_width="match_parent"
- android:layout_height="80dp"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:layout_marginTop="@dimen/dialog_hint_margintop"
- android:background="@drawable/fw_dl_input_et_nor"
- android:ems="10"
- android:gravity="top|left"
- android:hint="请填写内容"
- android:minHeight="50dp"
- android:padding="8dp"
- android:textColor="#bdbedf"
- android:textSize="@dimen/dialog_hint_textsize" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="@dimen/dialog_btn_hight"
- android:layout_marginBottom="14dp"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:layout_marginTop="@dimen/dialog_hint_margintop"
- android:gravity="center"
- android:minHeight="50dp"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/btn_ok"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_marginRight="20dp"
- android:layout_weight="1"
- android:background="@drawable/basedl_bt_ok"
- android:text="确定"
- android:textColor="@color/main_textcolor"
- android:textSize="@dimen/dialog_confirmbtn_textsize" />
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:background="@drawable/basedl_bt_cancle"
- android:text="取消"
- android:textColor="@color/main_textcolor"
- android:textSize="@dimen/dialog_confirmbtn_textsize" />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
父界面代码:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@android:color/white"
- android:gravity="center"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/btn_dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="对话框弹出" />
- <TextView
- android:id="@+id/tv_dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/btn_dialog"
- android:text="输入内容"
- android:textSize="20sp" />
- </RelativeLayout>
重写Dialog代码:
- package com.example.myalertdialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MyAlertDialog extends Dialog {
- private EditText intputEt;
- private Button sumitBtn;
- private Button cancelBtn;
- private Context context;
- public MyAlertDialog(Context context) {
- super(context, R.style.BaseDialogStyle);
- this.context = context;
- initDialog();
- }
- protected void initDialog() {
- setContentView(R.layout.my_dl_type_et);
- intputEt = (EditText) findViewById(R.id.anyfish_dialog_et_input);
- sumitBtn = (Button) findViewById(R.id.btn_ok);
- cancelBtn = (Button) findViewById(R.id.btn_cancel);
- cancelBtn.setOnClickListener(new android.view.View.OnClickListener() {
- @Override
- public void onClick(View v) {
- dismiss();
- }
- });
- }
- /**
- * 打开弹窗
- */
- public void onStartDiglog() {
- show();
- }
- /**
- * 关闭弹窗
- */
- public void onCloseDiglog(){
- dismiss();
- }
- /**
- * @return 返回输入值
- */
- public String getInputValue() {
- if (intputEt != null) {
- return String.valueOf(intputEt.getText()).trim();
- } else {
- return "";
- }
- }
- /**
- * @param inputValue
- * 设置输入的值
- */
- public void setInputValue(String inputValue) {
- if (intputEt != null && inputValue != null) {
- intputEt.setText(inputValue);
- }
- }
- /**
- * @param onClickListener
- * 设置的确认键监听事件
- */
- public void setSumitListener(
- android.view.View.OnClickListener onClickListener) {
- if (sumitBtn != null && onClickListener != null) {
- sumitBtn.setOnClickListener(onClickListener);
- }
- }
- }
ps:R.style.BaseDialogStyle为弹出框显示的效果
父activity代码:
- package com.example.myalertdialog;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private Button dialogBtn;
- private MyAlertDialog myAlertDialog;
- private TextView dialogTv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViews();
- initListener();
- }
- private void findViews(){
- dialogBtn=(Button) findViewById(R.id.btn_dialog);
- dialogTv=(TextView) findViewById(R.id.tv_dialog);
- myAlertDialog=new MyAlertDialog(this);
- }
- private void initListener(){
- myAlertDialog.setSumitListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- dialogTv.setText(myAlertDialog.getInputValue());
- myAlertDialog.onCloseDiglog();
- }
- });
- dialogBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- myAlertDialog.onStartDiglog();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
源代码下载地址:资源下载
Android中Dialog的使用的更多相关文章
- Android中Dialog
在Android中,Dialog是一个非常重要的UI, 它可以方便的给用户提示,用最简洁的方式向用户展示信息, 以下的图片是Dialog的一个整体架构,通过它,可以总体对Dialog有一个很清晰的认识 ...
- Android中Dialog对话框的调用及监听
Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...
- Android 中Dialog的使用
本文是参考ProAndroid的第10章Working with Dialogs的内容,在合适的地方添加了作者自己的一些见解最终成文. Android 中的对话框是一个展示在当前窗口上的小一号的窗口, ...
- Android中Dialog对话框
布局文件xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...
- 我的Android进阶之旅------>Android中Dialog系统样式讲解
今天在维护公司的一个APP的时候,有如下场景. 弹出一个AlertDialog的时候,在系统语言是中文的时候,如下所示: 弹出一个AlertDialog的时候,在系统语言是English的时候,如下所 ...
- android从Dialog对话框中取得文本文字
android中Dialog对话框获取文本文字,只需要使用editor的getText方法就可以获得,示例如下:final EditText et = new EditText(this); et.s ...
- Android 自定义Dialog类,并在Activity中实现按钮监听。
实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的 ...
- Android中制作自定义dialog对话框的实例
http://www.jb51.net/article/83319.htm 这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...
- 详细解读Android中的搜索框(二)—— Search Dialog
Search Dialog是提供搜索的控件之一,还有一个是上次小例子给出的searchView,关于SearchView的东西后面会说到.本次先从Search Dialog说起,让大家慢慢理解andr ...
随机推荐
- 【翻译】十大要避免的Ext JS开发方法
原文地址:http://www.sencha.com/blog/top-10-ext-js-development-practices-to-avoid/ 作者:Sean Lanktree Sean ...
- win32 字体变换与窗口同大同小
#include <windows.h> #include "res/resource.h" LRESULT CALLBACK WinProc(HWND hwnd, U ...
- 与众不同 windows phone (17) - Graphic and Animation(画图和动画)
原文:与众不同 windows phone (17) - Graphic and Animation(画图和动画) [索引页][源码下载] 与众不同 windows phone (17) - Grap ...
- 与众不同 windows phone (15) - Media(媒体)之后台播放音频
原文:与众不同 windows phone (15) - Media(媒体)之后台播放音频 [索引页][源码下载] 与众不同 windows phone (15) - Media(媒体)之后台播放音频 ...
- Java实现定时任务的三种方法(转)
在应用里经常都有用到在后台跑定时任务的需求.举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法: 普通thread实现 Tim ...
- 【iOS】iOS的iTunes文件共享,在程序Document路径
有时候程序开发须要通过沙盒中的 documents目录与用户共享文件,iTunes默认是不支持iTunes file Sharing的,首先设置 info-list的Application suppo ...
- HealthKit教程 Swift版:锻炼信息
原文:HealthKit Tutorial with Swift: Workouts 作者:Ernesto García 译者:Mr_cyz ) 欢迎回到我们的HealthKit系列教程! 在我们系列 ...
- Delphi的类型转换 good
Delphi是一种强类型转换的语言.在VC中,赋值符用″=″,例如x=1;到了Delphi赋值符就变成了″:=″,例如x:=1. 从赋值时用符号″:=″而不用″=″,就隐约可见Delphi对类型匹配要 ...
- shell 调试
感觉编写shell在查找错误的过程中,很让你崩溃,还好shell也提供了一些调试的方式: 语法检查 -n选项做语法检查,而不执行脚本 sh -n script_name.sh 启动 ...
- JAVA NIO 选择器
为什么要使用选择器 通道处于就绪状态后,就可以在缓冲区之间传送数据.可以采用非阻塞模式来检查通道是否就绪,但非阻塞模式还会做别的任务,当有多个通道同时存在时,很难将检查通道是否就绪与其他任务剥离开来, ...