我们在实际开发过程中,会遇到一个问题,我们的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();

最新文章

  1. .Net处理Oracle中Clob类型字段总结
  2. css样式增加&amp;改变颜色
  3. respberry pi3 上手随记
  4. 基于web的IM软件通信原理分析
  5. linux上ln链接命令详细说明
  6. 【BZOJ】【1008】【HNOI】越狱
  7. 在SSH整合框架中经常会用到Service层,请问大家这个Service层有什么具体的作用,可不可以不用这个Service层呢?
  8. android应用开发--------------看RadioGroup源代码,写相似单选选项卡的集成控件(如底部导航,tab等等)
  9. asp之vbscript函数
  10. maven下的sqlserver配置jar包
  11. linux下使用 FreeRDP 连接 Windows 远程桌面(转)
  12. bootstrap 幻灯大图结合dedecms的autoindex
  13. JS基础(二)
  14. Python--抽象类接口类
  15. CentOS7的网卡重启方法
  16. 01-Unity深入浅出(一)
  17. python - Linux C调用Python 函数
  18. Android外包团队——Jquery乱码解决方案
  19. 新版TeamTalk部署教程
  20. 一款基于css3的简单的鼠标悬停按钮

热门文章

  1. CentOS 6.4下Squid代理服务器的安装与配置,反向代理
  2. Microsoft SQL Server 混合云博客系列
  3. Foundation Sorting: Shellsort
  4. GDB学习之道:GDB调试精粹及使用实例
  5. SQL 注入与防御实例
  6. Expected stackmap frame at this location
  7. html 5 废弃的标签和属性
  8. Light OJ 1104 Birthday Pardo(生日悖论)
  9. 裸机代码(uboot) : clear bss
  10. linux安装LNMP的资源