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 ...
随机推荐
- SAP屏幕框架的创建
1.创建包括文本的基本框架 REPORT ztest_sum. TABLES:mara,syst. WITH FRAME TITLE mytitle. "mytitle是框架上的文本 ) A ...
- Java线程并发中常见的锁--自旋锁 偏向锁
随着互联网的蓬勃发展,越来越多的互联网企业面临着用户量膨胀而带来的并发安全问题.本文着重介绍了在java并发中常见的几种锁机制. 1.偏向锁 偏向锁是JDK1.6提出来的一种锁优化的机制.其核心的思想 ...
- POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)
题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件
本篇要登场的有三个控件,分别是滚轴控件.进度条控件和编辑控件. 一.滚轴控件 Ext.slider 1.滚轴控件的定义 下面我们定义三个具有代表意义滚轴控件,分别展示滚轴横向.纵向,以及单值.多值选择 ...
- JVM查找类文件的顺序(转)
配置classpath 根据path环境变量的原理,可以定义一个名为classpath环境变量,将要运行的class文件所在目录定义在该变量中. 例:set classpath=c:\ classpa ...
- 响应VC++ 标题栏右边的关闭按钮“红叉”
击标题栏右边的关闭按钮“红叉”时,程序会向窗口发送WM_CLOSE消息,因此可以截取此消息在窗口关系前做一些提示或者是不允许点击时关闭程序 case WM_CLOSE: if (...) { Post ...
- java学习笔记12--国际化
java学习笔记12--国际化 国际化的操作就是指一个程序可以同时适应多门语言,即:如果现在程序者是中国人,则会以中文为显示文字,如果现在程序的使用者是英国人,则会以英语为显示的文字,也就是说可以通过 ...
- GitHub 优秀Android 开源项目
阅读目录 1.Xabber客户端 2.oschina客户端 3.手机安全管家 4.星座连萌 5.玲闹铃 6.魔乐盒 7.PWP日历 8.Apollo音乐播放器 9.夏普名片识别 10.高仿人人网 11 ...
- NavigationBar 隐藏底部边线,阴影
NavigationBar 底部默认有一条边线 假设项目中须要隐藏何以採用这个库 https://github.com/samwize/UINavigationBar-Addition/
- cocos2d 走动椭圆
1.效果图 艺术与规划说他想与我合作在全国率先主角光环加,椭圆形走动. cocos2d自带没有,參考网上的写了一个. 2.椭圆数学知识 有关椭圆的数学知识我已经忘光了.网上找了点资料: a是椭圆的长半 ...