1 , Dialog布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical" > <TextView
android:id="@+id/title" //标题
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="14dp"
android:textColor="@color/login_hint"
android:textSize="@dimen/text_size_18" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp" > <TextView
android:id="@+id/confirm" //确认
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/btn_confirm_selector"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/text_size_16" /> <TextView
android:id="@+id/cancel" //取消
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@drawable/btn_cancel_selector"
android:gravity="center"
android:textColor="@color/login_hint"
android:textSize="@dimen/text_size_16" />
</LinearLayout> </LinearLayout>

2, 自己定义dialog 类

public class ConfirmDialog extends Dialog {

    private Context context;
private String title;
private String confirmButtonText;
private String cacelButtonText;
private ClickListenerInterface clickListenerInterface; public interface ClickListenerInterface { public void doConfirm(); public void doCancel();
} public ConfirmDialog(Context context, String title, String confirmButtonText, String cacelButtonText) {
super(context, R.style.MyDialog); // 定义的是样式 , 占时省略
this.context = context;
this.title = title;
this.confirmButtonText = confirmButtonText;
this.cacelButtonText = cacelButtonText;
} @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); init();
} public void init() {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.confirm_dialog, null);
setContentView(view); TextView tvTitle = (TextView) view.findViewById(R.id.title);
TextView tvConfirm = (TextView) view.findViewById(R.id.confirm);
TextView tvCancel = (TextView) view.findViewById(R.id.cancel); tvTitle.setText(title);
tvConfirm.setText(confirmButtonText);
tvCancel.setText(cacelButtonText); tvConfirm.setOnClickListener(new clickListener());
tvCancel.setOnClickListener(new clickListener()); Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
dialogWindow.setAttributes(lp);
} public void setClicklistener(ClickListenerInterface clickListenerInterface) {
this.clickListenerInterface = clickListenerInterface;
} private class clickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch (id) {
case R.id.confirm:
clickListenerInterface.doConfirm();
break;
case R.id.cancel:
clickListenerInterface.doCancel();
break;
}
}
};
}

3 ,使用

public static void Exit(final Context context) {
final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定要退出吗?", "退出", "取消");
confirmDialog.show();
confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
@Override
public void doConfirm() {
// TODO Auto-generated method stub
confirmDialog.dismiss(); // 关闭
//toUserHome(context);
AppManager.getAppManager().AppExit(context); //退出
} @Override
public void doCancel() {
// TODO Auto-generated method stub
confirmDialog.dismiss();
}
});
}

Dialog 自定义使用3(回调点击事件)的更多相关文章

  1. echart自定义浮窗 增加点击事件

    一:情景 做一个柱状图,需要在柱状图显示lable,并且浮窗上每个条目可以被点击或者跳转. 我使用的做图插件是echarts,但是echart的浮窗是图片,而且不可以被点击,不能识别html,而且这个 ...

  2. android 自定义listview无法响应点击事件OnItemClickListener

    如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我 ...

  3. flutter 处理dialog点击事件回调

    flutter 处理dialog点击事件回调 import 'package:flutter/material.dart'; import 'package:scoped_model/scoped_m ...

  4. 隐藏自定义的tabbar之后,push到B视图,B视图的键盘工具条无法响应点击事件

    我的情况如下: 在TabbarViewController中隐藏了系统的tabbar,然后自定义tabbar,A B C D 4个视图都有UINavigationController,A视图 使用的是 ...

  5. ListView使用自定义适配器的情况下实现适配器的文本和图标控件点击事件执行Activity界面中的方法

    ListView使用的是自定义适配器,列表项的布局文件中含有文本和图标,实现文本区域和图标区域的点击事件. 实现思路:在自定义适配器MyArrayAdapter 类型中自定义接口和接口方法,分别设置文 ...

  6. ListView使用自定义适配器的情况下实现适配器的控件点击事件执行Activity界面中的方法

    如果ListView使用的是自定义的适配器,比如MyArrayAdapter extends ArrayAdapter<String> 那么,如何实现适配器中的点击事件执行activity ...

  7. 关于百度地图InfoWindow响应自定义布局点击事件

    大概讲解: 在百度地图上显示一个marker,当marker被点击后,显示自定义的View.当自定义的View被点击后,响应不同Button的点击事件.被百度这个infowindo里面的view坑惨了 ...

  8. RecyclerView的点击事件添加-------接口回调的形式添加

    package com.example.recyclerviewdemo; import android.support.v7.widget.RecyclerView; import android. ...

  9. Android ---------高德卫星地图绘制多个点和点的点击事件自定义弹窗

    最近开发中,遇到一个多个点绘制,并实现点击事件,出现自定义窗口显示相关信息等功能,所以写了这篇博客. 从后台请求数据,得到多个经纬度,然后绘制在地图上,并实现点击,出现相关信息(自定义弹框实现) 先来 ...

随机推荐

  1. python基础5 ---python文件处理

    python文件处理 一.文件处理的流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件的操作方法 1.文件打开模式格式: 文件句柄 = open('文件路径', ...

  2. composer 更新国内镜像地址

    composer config -g repo.packagist composer https://packagist.phpcomposer.com​

  3. Docker 镜像篇

    镜像是 Docker 容器的基石,容器是镜像的运行实例,有了镜像才能启动容器. docker两个跟镜像有关的命令: hello-world - 最小的镜像 hello-world 是 Docker 官 ...

  4. float元素的父元素自适应高度

    当在对象内的盒子使用了float后,导致对象本身不能被撑开自适应高度,这个是由于浮动产生原因. 如何解决父div对象自适应高度,方法有三种. 1.对父元素设置固定高度 2.使用clear清除浮动 3. ...

  5. MHA高可用集群安装配置

    4台服务器 192.168.136.128 主 192.168.136.129 从 192.168.136.130 从 192.168.136.131 管理服务器 一主2从,一管理,安装mysql并配 ...

  6. 统计apachelog各访问状态个数(使用MapReduce)

    统计日志文件中各访问状态的个数. 1.将日志数据上传到hdfs 路径 /mapreduce/data/apachelog/in 中 内容如下 ::::::: - - [/Feb/::: +] :::: ...

  7. JVM中垃圾回收算法

    GC 算法与种类 GC的概念 Garbage Collection 垃圾收集1960年 List 使用了GCJava中,GC的对象是堆空间和永久区 引用计数法 老牌垃圾回收算法通过引用计算来回收垃圾使 ...

  8. Java -- AWT 画图,图像处理

    1. AWT画图  Graphics类  提供绘制简单图形的方法 更新图片时用到 repaint , update , 程序不应该主动调用paint和update, 这两个方法都应该是由AWT系统负责 ...

  9. Oracle结构控制语句

    --if语句 if [判断条件] then --条件满足执行的语句 end if; -- if ...else... if [判断条件] then ----条件满足执行的语句 else --不满足条件 ...

  10. 针对firefox ie6 ie7 ie8的css样式中的line-height属性

    针对firefox ie6 ie7 ie8的css样式中的line-height属性 以前我们大部分都是用!important来hack,对于ie6和firefox测试可以正常显示,但是ie7以上对! ...