Android编程心得-设计一个可重用的自定义Dialog
我们在实际开发过程中,会遇到一个问题,我们的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的更多相关文章
- Android编程心得-在任意类中获取当前屏幕宽高
进行Android编程时,很多时候都需要获取当前屏幕的宽度与高度,但是当我们需要在别的类中调用屏幕宽高时,直接用原来的方法是不行的,下面我来介绍如何在任意类中调用宽度高度的两种方法. public v ...
- Android学习笔记-构建一个可复用的自定义BaseAdapter
转载自http://www.runoob.com/w3cnote/android-tutorial-customer-baseadapter.html 作者:coder-pig 本节引言: 如题, ...
- 【转】[Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现
参考http://stackoverflow.com/questions/18460647/android-setfocusarea-and-auto-focus http://blog.csdn.n ...
- [Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现
写在前面 最近在从零开始写一个移动端的AR系统,坑实在是太多了!!!整个项目使用了OpenCV第三方库,但对于摄像机来说,和原生Camera的方法基本相同. 实现 以OpenCV的JavaCamera ...
- Android编程心得-ListView的Item高亮显示的办法
在我们使用ListView的时候,经常会遇到某一项(Item)需要高亮显示的情况,如下图,有人说当我们点击子项的时候会变亮,但有时候业务逻辑需要让ITEM根据条件自动变亮,下面我来介绍一下我自己的解决 ...
- Android编程心得-JSON使用心得(二)
在Android开发中,我们经常会用到JSON来与网络数据进行交互,下面我来介绍如何对JSON数据进行解析与制造 1.当我们需要对如下JSON串进行制造时: { "download" ...
- Android编程心得-Service数据绑定初步
在Android里,Service的数据绑定是一种重要的用法,我们知道Service与Activity一样是运行在当前应用进程的主线程里面的,他们之间交互的方式有多种,下面我来介绍一下如何使用数据绑定 ...
- Android编程心得-使用ActionBar+Fragment+ViewPager实现动态切换Menu效果
1.首先上效果图 2.本例实现的效果主要适用于当前页面有多个页签时.进行Fragment切换时,能够利用不同的Menu样式与当前Fragment中的内容进行配合,能够大大添加复用性,看到效果图后,以下 ...
- Android编程心得-Handler与子线程的交互初步
在编写项目的时候,本人发现一个关于线程与Handler很容易犯的错误. 我有两个Activity,一个Activity在后台创建了一个线程并且启动,这个线程对象对应的实体实在另外一个Activity的 ...
随机推荐
- char[]转换成wchar_t的转换方法(GNU Libc规定wchar_t为32位)
wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于unicode编码.unicode编码的字符一般以wchar_t类型存储. ...
- jQuery tmpl index
<!-- 校验失败后显示 TODO--> <script id="checkError_table_tmpl" type="text/x-handleb ...
- Java多线程编程中Future模式的详解
Java多线程编程中,常用的多线程设计模式包括:Future模式.Master-Worker模式.Guarded Suspeionsion模式.不变模式和生产者-消费者模式等.这篇文章主要讲述Futu ...
- 禁止apache显示目录索引
1)修改目录配置: 复制代码 代码如下: <Directory "D:/Apache/blog.phpha.com">Options Indexes FollowSym ...
- C#实现的内存分页机制的一个实例
C#实现的内存分页机制的一个实例 //多页索引表管理类(全局主索引表管理类) public class MuliPageIndexFeatureClass : IDisposable { protec ...
- s3c2440栈分配情况(fl2440裸机 stack)
//2440INIT.S ;The location of stacks UserStack EQU (_STACK_BASEADDRESS-0x3800) ;0x33ff4800 ~ SVCStac ...
- JavaSE学习总结第14天_API常用对象4
14.01 如何校验一个QQ号码案例 import java.util.Scanner; /* * 校验qq号码. * 1:要求必须是5-15位数字 * 2:0不能开头 * * 分析: * A:键 ...
- js方法中的this
比如有个function: function ServiceMy(services) { //存放this,用于调试用 var tmp_this = this; this.services = []; ...
- HTTP BIN测试
http://httpbin.org/ Tracing XML request/responses with JAX-WS: http://stackoverflow.com/questions/19 ...
- A package manager for Qt
官网 http://www.qpm.io/ A package manager for Qt 注释:这个网站类似JavaScript的包管理器的网站https://www.npmjs.com/ 都是给 ...