上一篇博文讲到对话框popWindow的使用,这篇博文主要解说Dialog的使用。

1、什么是Dialog?

Dialog就是对话框的一种方式!

在Android开发中。我们常常会须要在Android界面上弹出一些对话框,比方询问用户或者让用户选择。这样的对话框叫Dialog。最常常使用的,大家也比較熟悉的。也使用比較频繁有AlertDialog,这边篇博文将比較详尽的解说Dialog的使用。

2、Dialog的特性

Android的对话框有两种:PopupWindow和Dialog。它们的不同点在于:

Dialog的位置固定,而PopupWindow的位置能够任意。

Dialog是非堵塞线程的,而PopupWindow是堵塞线程的。

以上两点是PopupWindow和Dialog最大的不同。

3、AlertDialog的使用

通过查看这句代码AlertDialog定义的源代码

  1. 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界面代码为:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#60000000"
  6. android:gravity="center"
  7. android:orientation="vertical" >
  8.  
  9. <LinearLayout
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_marginLeft="20dp"
  13. android:layout_marginRight="20dp"
  14. android:background="@drawable/basedl_bg"
  15. android:focusable="true"
  16. android:focusableInTouchMode="true"
  17. android:orientation="vertical" >
  18.  
  19. <LinearLayout
  20. android:layout_width="fill_parent"
  21. android:layout_height="@dimen/dialog_face_hight"
  22. android:layout_marginLeft="20dp"
  23. android:layout_marginRight="20dp"
  24. android:layout_marginTop="20dp"
  25. android:background="@drawable/fw_dialog_signtype_blue"
  26. android:orientation="horizontal" >
  27.  
  28. <ImageView
  29. android:id="@+id/basedl_iv_head"
  30. android:layout_width="0dp"
  31. android:layout_height="fill_parent"
  32. android:layout_weight="2.5"
  33. android:padding="5dp"
  34. android:src="@drawable/ic_head_default" />
  35.  
  36. <LinearLayout
  37. android:layout_width="0dp"
  38. android:layout_height="fill_parent"
  39. android:layout_weight="7.5"
  40. android:orientation="vertical" >
  41.  
  42. <RelativeLayout
  43. android:layout_width="fill_parent"
  44. android:layout_height="0dp"
  45. android:layout_weight="1" >
  46.  
  47. <TextView
  48. android:id="@+id/anyfish_dialog_tv_hint"
  49. android:layout_width="fill_parent"
  50. android:layout_height="fill_parent"
  51. android:layout_alignParentLeft="true"
  52. android:gravity="bottom|left"
  53. android:paddingLeft="15dp"
  54. android:text="对话框名称"
  55. android:textColor="#ffffff" />
  56. </RelativeLayout>
  57.  
  58. <TextView
  59. android:id="@+id/basedl_tv_hint02"
  60. android:layout_width="fill_parent"
  61. android:layout_height="0dp"
  62. android:layout_weight="1"
  63. android:gravity="center_vertical|left"
  64. android:paddingLeft="15dp"
  65. android:text="对话框凝视。解释这个对话框作用"
  66. android:textColor="#ffffff"
  67. android:textSize="@dimen/dialog_fistype_exp_textsize" />
  68. </LinearLayout>
  69. </LinearLayout>
  70.  
  71. <EditText
  72. android:id="@+id/anyfish_dialog_et_input"
  73. android:layout_width="match_parent"
  74. android:layout_height="80dp"
  75. android:layout_marginLeft="20dp"
  76. android:layout_marginRight="20dp"
  77. android:layout_marginTop="@dimen/dialog_hint_margintop"
  78. android:background="@drawable/fw_dl_input_et_nor"
  79. android:ems="10"
  80. android:gravity="top|left"
  81. android:hint="请填写内容"
  82. android:minHeight="50dp"
  83. android:padding="8dp"
  84. android:textColor="#bdbedf"
  85. android:textSize="@dimen/dialog_hint_textsize" />
  86.  
  87. <LinearLayout
  88. android:layout_width="match_parent"
  89. android:layout_height="@dimen/dialog_btn_hight"
  90. android:layout_marginBottom="14dp"
  91. android:layout_marginLeft="20dp"
  92. android:layout_marginRight="20dp"
  93. android:layout_marginTop="@dimen/dialog_hint_margintop"
  94. android:gravity="center"
  95. android:minHeight="50dp"
  96. android:orientation="horizontal" >
  97.  
  98. <Button
  99. android:id="@+id/btn_ok"
  100. android:layout_width="0dp"
  101. android:layout_height="fill_parent"
  102. android:layout_marginRight="20dp"
  103. android:layout_weight="1"
  104. android:background="@drawable/basedl_bt_ok"
  105. android:text="确定"
  106. android:textColor="@color/main_textcolor"
  107. android:textSize="@dimen/dialog_confirmbtn_textsize" />
  108.  
  109. <Button
  110. android:id="@+id/btn_cancel"
  111. android:layout_width="0dp"
  112. android:layout_height="fill_parent"
  113. android:layout_weight="1"
  114. android:background="@drawable/basedl_bt_cancle"
  115. android:text="取消"
  116. android:textColor="@color/main_textcolor"
  117. android:textSize="@dimen/dialog_confirmbtn_textsize" />
  118. </LinearLayout>
  119. </LinearLayout>
  120.  
  121. </LinearLayout>

父界面代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@android:color/white"
  6. android:gravity="center"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context=".MainActivity" >
  12.  
  13. <Button
  14. android:id="@+id/btn_dialog"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="对话框弹出" />
  18.  
  19. <TextView
  20. android:id="@+id/tv_dialog"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@id/btn_dialog"
  24. android:text="输入内容"
  25. android:textSize="20sp" />
  26.  
  27. </RelativeLayout>

重写Dialog代码:

  1. package com.example.myalertdialog;
  2.  
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8.  
  9. public class MyAlertDialog extends Dialog {
  10.  
  11. private EditText intputEt;
  12. private Button sumitBtn;
  13. private Button cancelBtn;
  14. private Context context;
  15.  
  16. public MyAlertDialog(Context context) {
  17. super(context, R.style.BaseDialogStyle);
  18. this.context = context;
  19. initDialog();
  20. }
  21.  
  22. protected void initDialog() {
  23. setContentView(R.layout.my_dl_type_et);
  24. intputEt = (EditText) findViewById(R.id.anyfish_dialog_et_input);
  25. sumitBtn = (Button) findViewById(R.id.btn_ok);
  26. cancelBtn = (Button) findViewById(R.id.btn_cancel);
  27. cancelBtn.setOnClickListener(new android.view.View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. dismiss();
  31. }
  32. });
  33. }
  34.  
  35. /**
  36. * 打开弹窗
  37. */
  38. public void onStartDiglog() {
  39. show();
  40. }
  41.  
  42. /**
  43. * 关闭弹窗
  44. */
  45. public void onCloseDiglog(){
  46. dismiss();
  47. }
  48.  
  49. /**
  50. * @return 返回输入值
  51. */
  52. public String getInputValue() {
  53. if (intputEt != null) {
  54. return String.valueOf(intputEt.getText()).trim();
  55. } else {
  56. return "";
  57. }
  58. }
  59.  
  60. /**
  61. * @param inputValue
  62. * 设置输入的值
  63. */
  64. public void setInputValue(String inputValue) {
  65. if (intputEt != null && inputValue != null) {
  66. intputEt.setText(inputValue);
  67. }
  68. }
  69.  
  70. /**
  71. * @param onClickListener
  72. * 设置的确认键监听事件
  73. */
  74. public void setSumitListener(
  75. android.view.View.OnClickListener onClickListener) {
  76. if (sumitBtn != null && onClickListener != null) {
  77. sumitBtn.setOnClickListener(onClickListener);
  78. }
  79. }
  80.  
  81. }

ps:R.style.BaseDialogStyle为弹出框显示的效果

父activity代码:

  1. package com.example.myalertdialog;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10.  
  11. public class MainActivity extends Activity {
  12.  
  13. private Button dialogBtn;
  14. private MyAlertDialog myAlertDialog;
  15. private TextView dialogTv;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. findViews();
  21. initListener();
  22. }
  23.  
  24. private void findViews(){
  25. dialogBtn=(Button) findViewById(R.id.btn_dialog);
  26. dialogTv=(TextView) findViewById(R.id.tv_dialog);
  27. myAlertDialog=new MyAlertDialog(this);
  28. }
  29.  
  30. private void initListener(){
  31. myAlertDialog.setSumitListener(new OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. // TODO Auto-generated method stub
  35. dialogTv.setText(myAlertDialog.getInputValue());
  36. myAlertDialog.onCloseDiglog();
  37. }
  38. });
  39. dialogBtn.setOnClickListener(new OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. // TODO Auto-generated method stub
  43. myAlertDialog.onStartDiglog();
  44. }
  45. });
  46. }
  47.  
  48. @Override
  49. public boolean onCreateOptionsMenu(Menu menu) {
  50. // Inflate the menu; this adds items to the action bar if it is present.
  51. getMenuInflater().inflate(R.menu.main, menu);
  52. return true;
  53. }
  54.  
  55. }

源代码下载地址:资源下载

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. 【翻译】十大要避免的Ext JS开发方法

    原文地址:http://www.sencha.com/blog/top-10-ext-js-development-practices-to-avoid/ 作者:Sean Lanktree Sean ...

  2. win32 字体变换与窗口同大同小

    #include <windows.h> #include "res/resource.h" LRESULT CALLBACK WinProc(HWND hwnd, U ...

  3. 与众不同 windows phone (17) - Graphic and Animation(画图和动画)

    原文:与众不同 windows phone (17) - Graphic and Animation(画图和动画) [索引页][源码下载] 与众不同 windows phone (17) - Grap ...

  4. 与众不同 windows phone (15) - Media(媒体)之后台播放音频

    原文:与众不同 windows phone (15) - Media(媒体)之后台播放音频 [索引页][源码下载] 与众不同 windows phone (15) - Media(媒体)之后台播放音频 ...

  5. Java实现定时任务的三种方法(转)

    在应用里经常都有用到在后台跑定时任务的需求.举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法: 普通thread实现 Tim ...

  6. 【iOS】iOS的iTunes文件共享,在程序Document路径

    有时候程序开发须要通过沙盒中的 documents目录与用户共享文件,iTunes默认是不支持iTunes file Sharing的,首先设置 info-list的Application suppo ...

  7. HealthKit教程 Swift版:锻炼信息

    原文:HealthKit Tutorial with Swift: Workouts 作者:Ernesto García 译者:Mr_cyz ) 欢迎回到我们的HealthKit系列教程! 在我们系列 ...

  8. Delphi的类型转换 good

    Delphi是一种强类型转换的语言.在VC中,赋值符用″=″,例如x=1;到了Delphi赋值符就变成了″:=″,例如x:=1. 从赋值时用符号″:=″而不用″=″,就隐约可见Delphi对类型匹配要 ...

  9. shell 调试

    感觉编写shell在查找错误的过程中,很让你崩溃,还好shell也提供了一些调试的方式: 语法检查      -n选项做语法检查,而不执行脚本      sh -n script_name.sh 启动 ...

  10. JAVA NIO 选择器

    为什么要使用选择器 通道处于就绪状态后,就可以在缓冲区之间传送数据.可以采用非阻塞模式来检查通道是否就绪,但非阻塞模式还会做别的任务,当有多个通道同时存在时,很难将检查通道是否就绪与其他任务剥离开来, ...