上一篇博文讲到对话框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种。详细请查看我的博文:

7种形式的Android
AlertDialog使用举例

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的使用的更多相关文章

  1. Android中Dialog

    在Android中,Dialog是一个非常重要的UI, 它可以方便的给用户提示,用最简洁的方式向用户展示信息, 以下的图片是Dialog的一个整体架构,通过它,可以总体对Dialog有一个很清晰的认识 ...

  2. Android中Dialog对话框的调用及监听

    Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...

  3. Android 中Dialog的使用

    本文是参考ProAndroid的第10章Working with Dialogs的内容,在合适的地方添加了作者自己的一些见解最终成文. Android 中的对话框是一个展示在当前窗口上的小一号的窗口, ...

  4. Android中Dialog对话框

    布局文件xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  5. 我的Android进阶之旅------>Android中Dialog系统样式讲解

    今天在维护公司的一个APP的时候,有如下场景. 弹出一个AlertDialog的时候,在系统语言是中文的时候,如下所示: 弹出一个AlertDialog的时候,在系统语言是English的时候,如下所 ...

  6. android从Dialog对话框中取得文本文字

    android中Dialog对话框获取文本文字,只需要使用editor的getText方法就可以获得,示例如下:final EditText et = new EditText(this); et.s ...

  7. Android 自定义Dialog类,并在Activity中实现按钮监听。

      实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的 ...

  8. Android中制作自定义dialog对话框的实例

    http://www.jb51.net/article/83319.htm   这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...

  9. 详细解读Android中的搜索框(二)—— Search Dialog

    Search Dialog是提供搜索的控件之一,还有一个是上次小例子给出的searchView,关于SearchView的东西后面会说到.本次先从Search Dialog说起,让大家慢慢理解andr ...

随机推荐

  1. Eclipse中导入第三方源码的问题和备用解决方案

    在前篇<配置BeanUtils包,同时也是对导入第三包的步骤说明>中,我已经将[commons-beanutils-1.9.2.jar]包导入,但是在使用BeanUtils进行日期转换的过 ...

  2. TPL异步并行编程之取消任务

    TPL异步并行编程之简单使用 在上篇随笔里面说明了Task的使用,仅仅是简单使用,有时候把一个任务交给Task去执行,但是呢还是要管理下,比如说:我要叫这个任务停止了,不做了,任务取消了,或者超时了 ...

  3. VC/MFC 使edit控件不能进行粘贴操作

    这里使用消息拦截的方法 BOOL PersonDlg::PreTranslateMessage(MSG* pMsg) { if (GetDlgItem(IDC_EDIT_USER_ID)->m_ ...

  4. VC 绘图技巧--自定义形状图形

    自定义形状图形,定义几个点围城的图形,然后进行描边和填充: if (m_memDC.m_hDC!=NULL) { CPoint point[4]; point[0].x=nLeft+(int)(0.1 ...

  5. Mysql对自增主键ID进行重新排序

    Mysql数据库表的自增主键ID号经过一段时间的添加与删除之后乱了,需要重新排列. 原理:删除原有的自增ID,重新建立新的自增ID. 1,删除原有主键: ALTER TABLE `table_name ...

  6. 【Demo 0005】Java基础-类继承性

    本章学习要点:       1.  了解Java继承特性;       2.  掌握继承实现方法;       3.  掌握override规则: 一.类继承特性       1.  继承定义:使用己 ...

  7. javascript实现的可改变滚动方向的无缝滚动

    效果图如下: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  8. SVN的项目管理

    基于SVN的项目管理——集中与分散   我们在此处不讨论 GIT 比 SVN 好多少,也不讨论 Maven 和 Gradle 哪个好用,基于现有的开发环境,大多数公司还是采用 SVN + Maven ...

  9. 执行Asp.net应用程序在Linux上的3种托管方式

    执行Asp.net应用程序在Linux上的3种托管方式 想要执行Asp.net应用程序在Linux上.我们有3种选择: 1.使用Apache作为Webserver.使用mod_mono:http:// ...

  10. NetBeans工具学习之道:NetBeans的(默认)快捷键

    没什么好介绍的,是netbeans的快捷键,比較全面.看到好多坛子里还在问eclipse下的这个快捷键怎么netbeans下没有呢.曾经收集的,如今列在以下: 事实上,在当前安装的netbeans的 ...