我们在实际开发过程中,会遇到一个问题,我们的Dialog如果使用一般的方法进行设置调用出来,会有很多的重复代码,如何将Dialog按照自己的思路设计呢,并让其可重用呢,下面我来介绍一下我的方法

首先,设计Dialog的布局文件,代码如下,大家可以按照自己想要的方式设计适合自身APP的UI。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/exit_layout"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="@drawable/confirm_dialog_bg2" > <TextView
android:id="@+id/Dlg_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:padding="5dp"
android:textColor="#333"
android:textSize="20sp"
android:text="标题" /> <TextView
android:id="@+id/Dlg_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:layout_marginTop="1dp"
android:padding="10dp"
android:textSize="16sp"
android:gravity="center_horizontal"
android:text="内容\n" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:layout_marginBottom="8dp"
> <Button
android:id="@+id/Dlg_Yes"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="是"
android:textSize="16sp"
android:textColor="#fff"
android:background="@drawable/btn_style_green"
android:gravity="center" /> <Button
android:id="@+id/Dlg_No"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="否"
android:textSize="16sp"
android:textColor="#333"
android:background="@drawable/btn_style_white"
android:gravity="center" />
</LinearLayout> </LinearLayout>

重点来了,我们设计完布局文件后,接下来就是要编写自定义Dialog的逻辑了,这里需要注意的是这个五参的构造函数与回调方法,我们的实例通过构造函数实例化Dialog的样式,标题,内容与监听器,这里我的我只考虑了是与否两个按钮,同时他们有着自定义的接口CustomDialogListener,当CustomDialog的实例对象实例化时,点击是否按钮,对Dialog中自定义的OnClick方法回调,并让dialog,消失

public class CustomDialog extends Dialog  implements android.view.View.OnClickListener{

	TextView Dlg_title,Dlg_Content;
Button Dlg_Yes,Dlg_No;
CustomDialogListener cdListener;
String title,content;
// public CustomDialog(Context context, int theme) {
// super(context, theme);
// // TODO Auto-generated constructor stub
// } // Context context; public CustomDialog(Context context, int theme,String title,String content,CustomDialogListener cdListener){
super(context, theme);
this.cdListener=cdListener;
this.title=title;
this.content=content;
// this.context=context;
} @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.exit_dialog);
this.setCanceledOnTouchOutside(false); //点击外部不会消失
InitViews();
} private void InitViews(){
Dlg_title=(TextView) findViewById(R.id.Dlg_Title);
Dlg_Content=(TextView) findViewById(R.id.Dlg_content); Dlg_title.setText(this.title);
Dlg_Content.setText(this.content); Dlg_Yes=(Button) findViewById(R.id.Dlg_Yes);
Dlg_No=(Button) findViewById(R.id.Dlg_No);
Dlg_Yes.setOnClickListener(this);
Dlg_No.setOnClickListener(this);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
cdListener.OnClick(v);
dismiss();
} @Override
public void dismiss() {
// TODO Auto-generated method stub
System.out.println("dialog dismiss...");
super.dismiss();
} }
public interface CustomDialogListener {
public void OnClick(View view);
}

最后,介绍调用部分

			CustomDialog dialog=new CustomDialog(this, R.style.MyDialog, "退出应用","请选择是否退出应用\n",new CustomDialogListener() {

				@Override
public void OnClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.Dlg_Yes:
LoginActivity.this.finish();
break;
case R.id.Dlg_No: break; }
}
});
dialog.show();

Android编程心得-设计一个可重用的自定义Dialog的更多相关文章

  1. Android编程心得-在任意类中获取当前屏幕宽高

    进行Android编程时,很多时候都需要获取当前屏幕的宽度与高度,但是当我们需要在别的类中调用屏幕宽高时,直接用原来的方法是不行的,下面我来介绍如何在任意类中调用宽度高度的两种方法. public v ...

  2. Android学习笔记-构建一个可复用的自定义BaseAdapter

    转载自http://www.runoob.com/w3cnote/android-tutorial-customer-baseadapter.html   作者:coder-pig 本节引言: 如题, ...

  3. 【转】[Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现

    参考http://stackoverflow.com/questions/18460647/android-setfocusarea-and-auto-focus http://blog.csdn.n ...

  4. [Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现

    写在前面 最近在从零开始写一个移动端的AR系统,坑实在是太多了!!!整个项目使用了OpenCV第三方库,但对于摄像机来说,和原生Camera的方法基本相同. 实现 以OpenCV的JavaCamera ...

  5. Android编程心得-ListView的Item高亮显示的办法

    在我们使用ListView的时候,经常会遇到某一项(Item)需要高亮显示的情况,如下图,有人说当我们点击子项的时候会变亮,但有时候业务逻辑需要让ITEM根据条件自动变亮,下面我来介绍一下我自己的解决 ...

  6. Android编程心得-JSON使用心得(二)

    在Android开发中,我们经常会用到JSON来与网络数据进行交互,下面我来介绍如何对JSON数据进行解析与制造 1.当我们需要对如下JSON串进行制造时: { "download" ...

  7. Android编程心得-Service数据绑定初步

    在Android里,Service的数据绑定是一种重要的用法,我们知道Service与Activity一样是运行在当前应用进程的主线程里面的,他们之间交互的方式有多种,下面我来介绍一下如何使用数据绑定 ...

  8. Android编程心得-使用ActionBar+Fragment+ViewPager实现动态切换Menu效果

    1.首先上效果图 2.本例实现的效果主要适用于当前页面有多个页签时.进行Fragment切换时,能够利用不同的Menu样式与当前Fragment中的内容进行配合,能够大大添加复用性,看到效果图后,以下 ...

  9. Android编程心得-Handler与子线程的交互初步

    在编写项目的时候,本人发现一个关于线程与Handler很容易犯的错误. 我有两个Activity,一个Activity在后台创建了一个线程并且启动,这个线程对象对应的实体实在另外一个Activity的 ...

随机推荐

  1. [Python]Unicode转ascii码的一个好方法

    写这篇文章的是一位外国人,他遇到了什么问题呢?比如有一个 Unicode 字符串他需要转为 ascii码: >>> title = u"Klüft skräms inför ...

  2. Android JNI开发提高篇

    有关JNI的开发技术,我们继续围绕Android平台进行,JNI可以支持C或C++,从目前为止我们写过的JNI代码均为C实现的,即文件名为.C而C++的和这些有什么不同呢? Android平台上的JN ...

  3. bootstrap固定响应式导航

    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap. ...

  4. Android学习之Drawable(一)

    Drawable有很多种,它们表示一种图像概念,但它们不全是图片.Drawable是什么呢?下面是Google Android API中的定义: A Drawable is a general abs ...

  5. iOS内存管理 ARC与MRC

    想驾驭一门语言,首先要掌握它的内存管理特性.iOS开发经历了MRC到ARC的过程,下面就记录一下本人对iOS内存管理方面的一些理解. 说到iOS开发,肯定离不开objective-c语言(以下简称OC ...

  6. 新技能get: 使用whois查询不明网址的信息

    1.站长之家-->Whois反查 http://whois.chinaz.com/ 进入whois.chinaz.com,输入要查询的网址,选择查询即可.

  7. Qt 5.2.0 和 VS 2012集成

    下载两个安装包,后面一个add-in是必需的 Qt 5.2.0 for Windows 64-bit (VS 2012, 590 MB) (Info) Visual Studio Add-in 1.2 ...

  8. fieldset效果

    <form> <fieldset> <legend>健康信息</legend> 身高:<input type="text" / ...

  9. php随笔4-thinkphp 学习-ThinkPHP3.1快速入门(2)数据CURD

    ThinkPHP3.1快速入门(2)数据CURD   浏览:194739 发布日期:2012/09/05 分类:文档教程 关键字: 快速入门 CURD 上一篇中,我们了解了ThinkPHP的基础部分, ...

  10. 高级UNIX环境编程

    常规文件 只有索引节号(i-number,是一批i-node的索引),i-node不含文件名及数据字节,包括文件类型,链接数量,uid,gid,访问权限,字节数,最后访问时间,最后修改时间,信息节点最 ...