Dialog 自定义使用3(回调点击事件)
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(回调点击事件)的更多相关文章
- echart自定义浮窗 增加点击事件
一:情景 做一个柱状图,需要在柱状图显示lable,并且浮窗上每个条目可以被点击或者跳转. 我使用的做图插件是echarts,但是echart的浮窗是图片,而且不可以被点击,不能识别html,而且这个 ...
- android 自定义listview无法响应点击事件OnItemClickListener
如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我 ...
- flutter 处理dialog点击事件回调
flutter 处理dialog点击事件回调 import 'package:flutter/material.dart'; import 'package:scoped_model/scoped_m ...
- 隐藏自定义的tabbar之后,push到B视图,B视图的键盘工具条无法响应点击事件
我的情况如下: 在TabbarViewController中隐藏了系统的tabbar,然后自定义tabbar,A B C D 4个视图都有UINavigationController,A视图 使用的是 ...
- ListView使用自定义适配器的情况下实现适配器的文本和图标控件点击事件执行Activity界面中的方法
ListView使用的是自定义适配器,列表项的布局文件中含有文本和图标,实现文本区域和图标区域的点击事件. 实现思路:在自定义适配器MyArrayAdapter 类型中自定义接口和接口方法,分别设置文 ...
- ListView使用自定义适配器的情况下实现适配器的控件点击事件执行Activity界面中的方法
如果ListView使用的是自定义的适配器,比如MyArrayAdapter extends ArrayAdapter<String> 那么,如何实现适配器中的点击事件执行activity ...
- 关于百度地图InfoWindow响应自定义布局点击事件
大概讲解: 在百度地图上显示一个marker,当marker被点击后,显示自定义的View.当自定义的View被点击后,响应不同Button的点击事件.被百度这个infowindo里面的view坑惨了 ...
- RecyclerView的点击事件添加-------接口回调的形式添加
package com.example.recyclerviewdemo; import android.support.v7.widget.RecyclerView; import android. ...
- Android ---------高德卫星地图绘制多个点和点的点击事件自定义弹窗
最近开发中,遇到一个多个点绘制,并实现点击事件,出现自定义窗口显示相关信息等功能,所以写了这篇博客. 从后台请求数据,得到多个经纬度,然后绘制在地图上,并实现点击,出现相关信息(自定义弹框实现) 先来 ...
随机推荐
- oldboyshell编程扩展内容
oldboyshell编程扩展内容一.命令的优先级 命令分为: ==> alias ==> Compound Commands ==> function ==> build_ ...
- 10 个经典PHP函数
这篇文章主要介绍了php中的10个比较经典的函数,不太常见,可以满足有特殊需求的朋友 1. sys_getloadavg() sys_getloadavt()可以获得系 统负载情况.该函数返回一个包含 ...
- PAT 天梯赛 【】 L3-015. 球队“食物链” 【BFS+剪枝】
题目链接 https://www.patest.cn/contests/gplt/L3-015 思路 用一个 数组标记 胜负 每次输入一行字符串 然后遍历 如果 碰到 W 那么 vis[i][j] = ...
- Swift URL encode
前言 在WEB前端开发,服务器后台开发,或者是客户端开发中,对URL进行编码是一件很常见的事情,但是由于各个年代的RFC文档中的内容一直在变化,一些年代久远的代码就对URL编码和解码的规则和现在的有一 ...
- 每天一个Linux命令(25)chgrp命令
chgrp命令用来改变文件或目录所属的用户组. (1)用法: 用法: chgrp [选项参数] [组] [文件] 或 chgrp [选项] 组文件... POSIX ...
- how to close the old Session - if the same username starts a new Session?
Question: want to close the old Session - if the same username starts a new Session Any ideas how i ...
- HBase常用操作-HBaseUtil
package com.zhen.hbase; import java.io.IOException; import java.util.ArrayList; import java.util.Col ...
- codevs1217 借教室
借教室(classroom.cpp/c/pas)[问题描述]在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室 ...
- 分析CSS布局中BFC
1.什么是BFC BFC(Block Formatting Context,块级元素格式化上下文)是 W3C CSS 2.1 规范中的一个概念,它决定了元素如何对其内容进行定位,以及与其他元素的关系和 ...
- LSM Tree 学习笔记——本质是将随机的写放在内存里形成有序的小memtable,然后定期合并成大的table flush到磁盘
The Sorted String Table (SSTable) is one of the most popular outputs for storing, processing, and ex ...