Android 自定义Dialog类,并在Activity中实现按钮监听事件
来源:互联网 发布:矩阵连乘a1 a2 编辑:程序博客网 时间:2024/05/17 23:49
实际开发中,经常会用到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);
0 0
- Android 自定义Dialog类,并在Activity中实现按钮监听事件
- Android自定义Dialog类,并在Activity中实现按钮监听
- 自定义Dialog如何监听按钮事件
- 自定义Dialog以及按钮事件监听
- Android 自定义Dialog,自定义接口监听事件。
- Android自定义Dialog实现弹出它的Activity背景变暗,并自定义Dialog的大小
- Android中自定义ScrollView的滑动监听事件,并在滑动时渐变标题栏背景颜色
- 按钮的监听事件在fragment中如何实现
- 在android 中利用button 按钮设置监听事件
- Android Dialog创建及其监听事件实现
- Android开发:自定义界面的Dialog,并且实现按钮的监听回调
- Android界面之----自定义的Dialog,然后利用回调方法,在调用处,进行Dialog中各按钮的事件处理。
- 在Android中实现自定义的按钮
- Android之在Dialog中添加单选按钮【自定义Dialog】
- android,自定义dialog,自定义按钮,事件处理问题。
- activity获取viewpager中fragment里的button按钮,并设置监听怎么实现
- 安卓自定义dialog,改变其在主页面的位置和实现dialog上的按钮点击事件
- android dialog在activity中,如何处理activity捕捉不到back点击事件
- PostgreSQL 中文资料汇总
- User account has expired
- poj1001
- Linux内核Crash分析
- Python class
- Android 自定义Dialog类,并在Activity中实现按钮监听事件
- 过滤关联表属性
- 关于九宫格的适配布局
- Linux下Graphite的安装及部署
- 数组排序
- 序列化
- Python 实现进程锁
- Hibernate 缓存机制
- 同行代码审查之我见