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中this的一点点理解
1 当函数作为对象的方法被调用的时候 this就指向该对象 var o = { prop: 37, f: function() { return this.prop; } }; console.log ...
- Mac常用shell命令
几个常用命令 pwd 命令名称:pwd 英文:print work directory 描述:查看当前工作目录的完整路径 ls 英文全称: list 描述:列出目录下的内容清单 常用参数: -l:列出 ...
- MySQL的表使用
-- 创建表CREATE TABLE teacher( id INT, NAME VARCHAR(20))-- 查看所有表SHOW TABLES; DESC student; DROP TABLE s ...
- 微信小程序 - 开发指南
一.下载并安装开发工具 下载地址 二.创建项目 打开开发工具 添加项目 进入预览和调试界面 代码编辑器 编译并预览 三.启动流程 四.适用场景 五.技术框架 六.科普 [图片较大 - 点击查看]
- yii2缓存的介绍和使用
作者:白狼 出处:http://www.manks.top/yii2_cache.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律 ...
- vim 使用 YouCompleteMe
当然前提是先装好vundle 1 在vimrv中加入Bundle 'Valloric/YouCompleteMe' 2 vim +PluginInstall +qall 3 安装一对底层需要的编译的东 ...
- -bash: ulimit: pipe size: cannot modify limit: Invalid argument
从root账号切换到oracle账号时,出现了"-bash: ulimit: pipe size: cannot modify limit: Invalid argument"提示 ...
- 15天玩转redis —— 第十一篇 让你彻底了解RDB存储结构
接着上一篇说,这里我们来继续分析一下RDB文件存储结构,首先大家都知道RDB文件是在redis的“快照”的模式下才会产生,那么如果 我们理解了RDB文件的结构,是不是让我们对“快照”模式能做到一个心中 ...
- Oracle索引梳理系列(五)- Oracle索引种类之表簇索引(cluster index)
版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...
- html iframe 元素之间的调用
html iframe 元素之间的调用一.简介 一般需要引入一个独立页面的时候,我们会使用iframe.在业务需要的时候,我们需要在父页面与iframe页面之间进行交互.交互的时候,我们就需要使 用到 ...