实际开发中,经常会用到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中实现按钮监听。的更多相关文章

  1. 安卓入门 使用android创建一个项目 从启动activity中响应按钮事件 启动另一个activity 并传递参数

    启动android studio创建一个新项目 public void sendMessage(View view){ Intent intent=new Intent(this,DispalyMes ...

  2. Android 自定义dialog类

    首先定制style样式 styles.xml 加入自定义样式 <style name="CustomLoadingDialog"> <item name=&quo ...

  3. Android 自定义EditText实现粘贴,复制,剪切的监听

    package com.dwtedx.qq.view; import android.annotation.SuppressLint; import android.content.Context; ...

  4. Android PopupWindow Dialog 关于 is your activity running 崩溃详解

    Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...

  5. Android 自定义View及其在布局文件中的使用示例(二)

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3530213.html From crash_coder linguowu linguowu0622@gami ...

  6. Android自定义Dialog及其布局

     实际项目开发中默认的Dialog样式无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件. 上效果图: 自定义Dialog,LogoutDialog: 要将自定义布局传入构造函数中, ...

  7. Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...

  8. Android自定义 Dialog 对话框

    Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...

  9. android 自定义Dialog去除黑色边框

    在自定义Dialog时显示的界面中老是有黑色的边框,下面就介绍使用style去除黑色边框方法. 首先在values/styles定义自定义样式: <style name="MyDial ...

随机推荐

  1. Maven学习随笔一——Maven安装报错处理(mvn -v, 提示不是内部命令的问题)

    今天心血来潮学习maven,可是光安装就花了个把小时,好坑有木有! 安装过程可百度,各种经贴,不详. 控制台输入  mvn -v ,如果报错,很可能是你的java/maven的环境变量配置出了点问题: ...

  2. 有问题得就分享(此实现不是 Windows 平台 FIPS 验证的加密算法的一部分)

    今天呢,我写完了MD5加密算法的代码,可是我正高兴的想运行时,按了F5.突然了个这样的窗体 我当时就纳闷了,怎么回事? 代码又没有错,这是乍回事! 于是呢上网查了看看:可以这样解决的 在window中 ...

  3. Ajax实现简单下拉选项

    基本都是固定步骤!主要在JAVASCRIPT和PHP中的操作 1.HTML代码里就只有两个SELECT标签如下: <select id="province"> < ...

  4. iOS之处理不等高TableViewCell的几种方法

    课题一:如何计算Cell高度 方案一:直接法(面向对象) 直接法,就是把数据布局到Cell上,然后拿到Cell最底部控件的MaxY值. 第一步:创建Cell并正确设置约束,使文字区域高度能够根据文字内 ...

  5. EditText 显示明文和密码

    1.效果图 2.布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xml ...

  6. 【iOS】Alamofire库在iOS7下设置Head无效的问题

    声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 同样的代码在iOS8下没有问 ...

  7. 【代码笔记】iOS-获取字符串的宽度,高度

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...

  8. 基于Ruby的watir-webdriver自动化测试方案与实施(四)

    接着基于Ruby的watir-webdriver自动化测试方案与实施(三) http://www.cnblogs.com/Javame/p/4159468.html 继续 ... ...   首先回忆 ...

  9. Mindjet MindManager思维导图工具的使用 - 项目管理系列文章

    在项目管理过程中,不可避免的要使用到各种各样的项目管理工具.本文就对我自己使用的Mindjet Manager进行一下介绍,推荐给大家使用. 1.  打开软件 这里使用了2012版 2.  软件的界面 ...

  10. vimrc

    我的vimrc https://github.com/juandx/vimrc 当然得装vundle git clone https://github.com/VundleVim/Vundle.vim ...