Android 自定义Dialog类,并在Activity中实现按钮监听。
实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下:

开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的按钮,Dialog类如下,在MyDialog这个类中实现了一个LeaveMyDialogListener接口,用来实现onclick的点击事件:
package com.Ieasy.Tool;
import com.Ieasy.ieasyware.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog implements android.view.View.OnClickListener { private Context context;
private TextView txt;
private Button btnok,btnedit,btncancle,btnsave;
private LeaveMyDialogListener listener; public interface LeaveMyDialogListener{
public void onClick(View view);
} public MyDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
} public MyDialog(Context context,int theme,LeaveMyDialogListener listener) {
super(context,theme);
// TODO Auto-generated constructor stub
this.context = context;
this.listener = listener;
} @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(com.Ieasy.ieasyware.R.layout.mydialog);
btncancle = (Button)findViewById(R.id.mycancle);
btnedit = (Button)findViewById(R.id.myedit);
btnok = (Button)findViewById(R.id.myok);
txt = (TextView)findViewById(R.id.miaosu);
btnsave = (Button)findViewById(R.id.mysave);
btncancle.setOnClickListener(this);
btnedit.setOnClickListener(this);
btnok.setOnClickListener(this);
btnsave.setOnClickListener(this);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
listener.onClick(v);
}
}
布局文件如下:
<?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="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/night_biz_subscribe_media_recommend_item_bg"
android:orientation="vertical" > <TextView
android:id="@+id/miaosu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="描述"
android:textColor="@color/whitesmoke"
android:textSize="20sp" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:orientation="horizontal" > <Button
android:id="@+id/myok"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btnclick"
android:textColor="@color/whitesmoke"
android:text="确定" /> <Button
android:id="@+id/myedit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btnclick"
android:textColor="@color/whitesmoke"
android:text="编辑" /> <Button
android:id="@+id/mysave"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btnclick"
android:textColor="@color/whitesmoke"
android:text="保存" /> <Button
android:id="@+id/mycancle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btnclick"
android:textColor="@color/whitesmoke"
android:text="取消" /> </LinearLayout>
</LinearLayout> </LinearLayout>
引用的style:
<style name="MyDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/night_biz_subscribe_media_recommend_item_bg</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
最后在Activity中调用,通过LeaveMyDialogListener 接口来实现在Activity中的点击事件
MyDialog dialog = new MyDialog(context,R.style.MyDialog,
new MyDialog.LeaveMyDialogListener() {
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.myok:
break;
case R.id.myedit:
break;
case R.id.mycancle: break;
case R.id.mysave:
dialog.dismiss(); default:
break;
}
}
});
dialog.show();
如果想获得Dialog中的TextView控件可以这样获取,给TextView赋值时候一定要在Dialog show了之后在赋值,你懂得。
TextView text = (TextView) dialog.findViewById(R.id.miaosu);
Android 自定义Dialog类,并在Activity中实现按钮监听。的更多相关文章
- 安卓入门 使用android创建一个项目 从启动activity中响应按钮事件 启动另一个activity 并传递参数
启动android studio创建一个新项目 public void sendMessage(View view){ Intent intent=new Intent(this,DispalyMes ...
- Android 自定义dialog类
首先定制style样式 styles.xml 加入自定义样式 <style name="CustomLoadingDialog"> <item name=&quo ...
- Android 自定义EditText实现粘贴,复制,剪切的监听
package com.dwtedx.qq.view; import android.annotation.SuppressLint; import android.content.Context; ...
- Android PopupWindow Dialog 关于 is your activity running 崩溃详解
Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...
- Android 自定义View及其在布局文件中的使用示例(二)
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3530213.html From crash_coder linguowu linguowu0622@gami ...
- Android自定义Dialog及其布局
实际项目开发中默认的Dialog样式无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件. 上效果图: 自定义Dialog,LogoutDialog: 要将自定义布局传入构造函数中, ...
- Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...
- Android自定义 Dialog 对话框
Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...
- android 自定义Dialog去除黑色边框
在自定义Dialog时显示的界面中老是有黑色的边框,下面就介绍使用style去除黑色边框方法. 首先在values/styles定义自定义样式: <style name="MyDial ...
随机推荐
- js 假值
function demo(a){ if(a){ console.log(111); }else{ console.log(222); } } demo(0) html_dom.html:27 222 ...
- 配置nginx支持ssl服务器—HTTPS
下文摘自: http://docs.bigbluebutton.org/install/install.html Configuring HTTPS on BigBlueButtonAncho ...
- iOS FFmpeg 优秀博客(资源)集锦
iOS FFmpeg 优秀博客(资源)集锦 这篇博客没有我自己写的内容: 主要是对FFmpeg一些优秀博客的记录 随时更新 1>iOS编译FFmpeg,kxmovie实现视频播放 2>视音 ...
- SQL Server解决孤立用户浅析
孤立用户概念 所谓孤立用户即指在服务器实例上未定义或错误定义了其相应 SQL Server 登录名的数据库用户无法登录到实例. 这样的用户被称为此服务器实例上的数据库的"孤立用 ...
- com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Cannot assign requested address: bind
在发布 web service 时报错: Endpoint.publish(publishAddress, hl7MessageReveiver); com.sun.xml.internal.ws.s ...
- PHP函数整理(二)
以下均参考自 php.net 1.curl_setopt_array() 此函数为CURL传输会话批量设置选项.这个函数对于需要设置大量的curl选项是非常有用的,不需要重复的调用curl_setop ...
- 常用python机器学习库总结
开始学习Python,之后渐渐成为我学习工作中的第一辅助脚本语言,虽然开发语言是Java,但平时的很多文本数据处理任务都交给了Python.这些年来,接触和使用了很多Python工具包,特别是在文本处 ...
- XML 序列化与反序列化
XML序列化与反序列化 1.将一个类转化为XML文件 /// <summary> /// 对象序列化成XML文件 /// </summary> /// <param na ...
- ipv4理论知识2-分类编址、ip分类、网络标识、主机标识、地址类、地址块
分类编址 ipv4的体系结构中有分类编址和无分类编址(后续会介绍到),在分类编址时,ipv4地址分为A.B.C.D.E这5类.每类占用的IP比例和个数如下图: ipv4分类识别 计算机以二进制方式存储 ...
- 基础SQL语句/语法
SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库 Create DATABAS ...