实际开发中,经常会用到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. [Cordova] Plugin里使用iOS Framework

    [Cordova] Plugin里使用iOS Framework 前言 开发Cordova Plugin的时候,在Native Code里使用第三方Library,除了可以加速项目的时程.也避免了重复 ...

  2. JS 预解释相关理解

    1.JS中的内存空间分为两种:栈内存.堆内存 栈内存:提供JS代码执行的环境;存储基本数据类型的值; ->全局作用域或者私有的作用域其实都是栈内存 堆内存:存储引用数据类型的值(对象是把属性名和 ...

  3. H5学习

    1.html{font-size:62.5%;}//不用font-size:10px的原因:(因为设了62.5%后就有1rem = 10px,便于用rem来指定元素的尺寸,这样响应式的时候可以直接改变 ...

  4. EF UoC

    The Repository Pattern with EF Code First & Dependency Injection in ASP.NET MVC3 Ray_Liang, 5 Ju ...

  5. Android开发学习——SQLite数据库与单元测试

    SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHelper  public class Myopenhelper extends SQLiteOpenHelp ...

  6. IOS Emoji表情

    IOS Emoji 前言:我比较喜欢有趣的东西,有一些有趣的小东西,可能不是多么多么牛逼,也可能不需要多高深的技巧,也不会为其他什么强大的功能而服务,但是有时候将很多有趣的小东西组合起来运用,比如在你 ...

  7. 11-C语言指针&一维数组&字符串

    一.用指针遍历数组元素 1.最普通的遍历方式是用数组下标来遍历元素 1 // 定义一个int类型的数组 2 int a[4] = {1, 2, 3, 4}; 3 4 int i; 5 for (i = ...

  8. SE(homework2)_软件分析

    老师这次课后的作业具有开放性,很容易的我会想到经常用的那些工具软件,MATLAB,envi,ARCGIS等等. Q1:此类软件是什么时候出现的,这些软件是怎么说服你(陌生人)成为它们的用户的?他们的目 ...

  9. crontab设置作业间隔执行问题

    这个源于有个网友问,crontab里   */40  9  *  *  *  是不是代表9点开始每40分钟执行?  直觉这个肯定不是从9:00开始每隔40分钟执行一次,但是也不能肯定它代表一个什么样的 ...

  10. Oracle创建表空间和用户

    用户名为:C##NEWO ============================================================================= /*分为四步 */ ...