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 ...
随机推荐
- C#多线程实现方法——Task/Task.Factary
原文:C#多线程实现方法--Task/Task.Factary Task 使用 Task以及Task.Factory都是在.Net 4引用的.Task跟Thread很类似,通过下面例子可以看到. st ...
- 获取ocx运行路径的另一种方法
在InitInstance里边可以获取 1 2 3 4 5 6 7 8 9 10 11 12 if (bInit) { // TODO: 在此添加您自己的模块初始化 ...
- Redis intset
Redis intset也非常简单.它是一个有序整数集合,实际上是一个有序数组,查找时二分查找,添加时可能需要内存挪动.唯一不同的是,它的整数类型是变动的,可以支持int16,int32,int64三 ...
- 分析javascript关闭
1.什么是闭包 1)官方解释 一个拥有多个变量和绑定了这些变量的环境的表达式(一般是一个函数).因而这些变量也是该表达式的一部分. 我的理解:所谓的闭包就是连接函数内部和函数外部的一座桥梁.使得在外部 ...
- Mac必备软件推荐
阅读原文http://littlewhite.us/archives/245 随着IOS的流行.Mac电脑也越来越多的进入人们的视野,和iPhone系列一样,苹果的Mac产品线也是软硬件完美结合.有着 ...
- 【源代码】基于Android和蓝牙的单片机温度採集系统
如需转载请标明出处:http://blog.csdn.net/itas109 QQ技术交流群:129518033 STC89C52单片机通过HC-06蓝牙模块与Android手机通信实例- 基于And ...
- javascript (二) 事件
<script></script> 函数写法: function fun_name(){ x=docment.getElementById("demo") ...
- Common Lisp Style Guide - Ariel Networks Labs
Common Lisp Style Guide - Ariel Networks Labs Common Lisp Style Guide
- fck编辑器的使用
FCK编辑器的使用 注意:编辑器有浏览器缓存,所以修改配置后,一定要删一下缓存 这个编辑器是采用 html+javascript 开发出来的 通常作为插件来使用: 1,下载插件包 2,解压,加压之后看 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 站点设置
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 站点设置 你应该熟悉(假设还咩有)的SharePo ...