我们在实际开发过程中,会遇到一个问题,我们的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. POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  2. c 围圈报数

    #include<stdio.h> /*围圈报数*/ void left(int *p,int m,int n) { int i,j,count; i = j = count = ; ) ...

  3. SQL Server调试常用参数

    SQL Server 清除缓存: DBCC DROPCLEANBUFFERS 从缓冲池中删除所有清除缓冲区. DBCC FREEPROCCACHE 从过程缓存中删除所有元素. DBCC FREESYS ...

  4. 在ASP.NET中动态加载内容(用户控件和模板)

    在ASP.NET中动态加载内容(用户控件和模板) 要点: 1. 使用Page.ParseControl 2. 使用base.LoadControl 第一部分:加载模板 下 面是一个模板“<tab ...

  5. Arrays类学习笔记

    Arrays.asList(arr); 该方法可以把数组变成List集合. String[] arr = {"abc","cc"}; List<Strin ...

  6. 菜鸟初识UML

    首当其冲的就是:什么是UML呢? 首先,UML 是一种可视化的面向对象的建模语言.它是一个支持模型化和软件系统开发的图形化语言,为软件开发的所有阶段提供模型化和可视化支持,包括由需求分析到规格,到构造 ...

  7. 我用的比较少的CSS选择器

    选择器 描述 [attribute] 用于选取带有指定属性的元素. [attribute=value] 用于选取带有指定属性和值的元素. [attribute~=value] 用于选取属性值中包含指定 ...

  8. LinkList Operation

    链表典型数据结构: #define ElemType int typedef struct LinkNode{ ElemType value; struct LinkNode* next; }; 相比 ...

  9. Winform 绘制圆形的图片

    string filename = "icon.png";//如果不是png类型,须转换 System.Drawing.Bitmap bitmap = new System.Dra ...

  10. struts.xml的配置

    <?xml version="1.0" encoding="UTF-8"?> <!--第一行必须这样写,这句话必须放在第一行--> &l ...