Android实现可输入数据的弹出框
程序员文章站
2024-02-13 09:51:22
之前一篇文章,介绍了如何定义从屏幕底部弹出popupwindow即《android animation实战之屏幕底部弹出popupwindow》,写完之后,突然想起之前写过...
之前一篇文章,介绍了如何定义从屏幕底部弹出popupwindow即《android animation实战之屏幕底部弹出popupwindow》,写完之后,突然想起之前写过自定义内容显示的弹出框,就随手写了两个实例,分享出来:
第一种实现方式:继承dialog
1.1 线定义弹出框要显示的内容:create_user_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/create_user_dialog_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/dialog_load_bg" android:minwidth="200dp" android:orientation="vertical" android:padding="10dp" android:paddingbottom="30dp" android:paddingtop="30dp"> <edittext android:id="@+id/text_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/edit_bg" android:hint="姓名" android:minheight="45dp" android:textsize="18sp" /> <edittext android:id="@+id/text_mobile" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:background="@drawable/edit_bg" android:hint="手机号" android:minheight="45dp" android:textsize="18sp" /> <edittext android:id="@+id/text_info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:background="@drawable/edit_bg" android:gravity="top|left" android:hint="个性签名" android:minheight="145dp" android:textsize="18sp" /> <button android:id="@+id/btn_save_pop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:text="保存" /> </linearlayout>
1.2 定义要弹出的dialog
public class createuserdialog extends dialog { /** * 上下文对象 * */ activity context; private button btn_save; public edittext text_name; public edittext text_mobile; public edittext text_info; private view.onclicklistener mclicklistener; public createuserdialog(activity context) { super(context); this.context = context; } public createuserdialog(activity context, int theme, view.onclicklistener clicklistener) { super(context, theme); this.context = context; this.mclicklistener = clicklistener; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // 指定布局 this.setcontentview(r.layout.create_user_dialog); text_name = (edittext) findviewbyid(r.id.text_name); text_mobile = (edittext) findviewbyid(r.id.text_mobile); text_info = (edittext) findviewbyid(r.id.text_info); /* * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getwindow(),表示获得这个activity的window * 对象,这样这可以以同样的方式改变这个activity的属性. */ window dialogwindow = this.getwindow(); windowmanager m = context.getwindowmanager(); display d = m.getdefaultdisplay(); // 获取屏幕宽、高用 windowmanager.layoutparams p = dialogwindow.getattributes(); // 获取对话框当前的参数值 // p.height = (int) (d.getheight() * 0.6); // 高度设置为屏幕的0.6 p.width = (int) (d.getwidth() * 0.8); // 宽度设置为屏幕的0.8 dialogwindow.setattributes(p); // 根据id在布局中找到控件对象 btn_save = (button) findviewbyid(r.id.btn_save); // 为按钮绑定点击事件监听器 btn_save.setonclicklistener(mclicklistener); this.setcancelable(true); } }
1.3 调用弹出框
public void showeditdialog(view view) { createuserdialog = new createuserdialog(this,r.style.loading_dialog,onclicklistener); createuserdialog.show(); } private view.onclicklistener onclicklistener = new view.onclicklistener() { @override public void onclick(view v) { switch (v.getid()) { case r.id.btn_save: string name = createuserdialog.text_name.gettext().tostring().trim(); string mobile = createuserdialog.text_mobile.gettext().tostring().trim(); string info = createuserdialog.text_info.gettext().tostring().trim(); system.out.println(name+"——"+mobile+"——"+info); break; } } };
第二种实现方式:继承popupwindow
2.1 定义弹出框布局文件,和1.1定义的一致
2.2 定义要弹出的popupwindow
public class createuserpopwin extends popupwindow { private context mcontext; private view view; private button btn_save_pop; public edittext text_name; public edittext text_mobile; public edittext text_info; public createuserpopwin(activity mcontext, view.onclicklistener itemsonclick) { this.mcontext = mcontext; this.view = layoutinflater.from(mcontext).inflate(r.layout.create_user_pop, null); text_name = (edittext) view.findviewbyid(r.id.text_name); text_mobile = (edittext) view.findviewbyid(r.id.text_mobile); text_info = (edittext) view.findviewbyid(r.id.text_info); btn_save_pop = (button) view.findviewbyid(r.id.btn_save_pop); // 设置按钮监听 btn_save_pop.setonclicklistener(itemsonclick); // 设置外部可点击 this.setoutsidetouchable(true); /* 设置弹出窗口特征 */ // 设置视图 this.setcontentview(this.view); // 设置弹出窗体的宽和高 /* * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getwindow(),表示获得这个activity的window * 对象,这样这可以以同样的方式改变这个activity的属性. */ window dialogwindow = mcontext.getwindow(); windowmanager m = mcontext.getwindowmanager(); display d = m.getdefaultdisplay(); // 获取屏幕宽、高用 windowmanager.layoutparams p = dialogwindow.getattributes(); // 获取对话框当前的参数值 this.setheight(relativelayout.layoutparams.wrap_content); this.setwidth((int) (d.getwidth() * 0.8)); // 设置弹出窗体可点击 this.setfocusable(true); } }
2.3 调用该弹框组件
public void showeditpopwin(view view) { createuserpopwin = new createuserpopwin(this,onclicklistener); createuserpopwin.showatlocation(findviewbyid(r.id.main_view), gravity.center, 0, 0); } private view.onclicklistener onclicklistener = new view.onclicklistener() { @override public void onclick(view v) { switch (v.getid()) { case r.id.btn_save_pop: string name1 = createuserpopwin.text_name.gettext().tostring().trim(); string mobile1 = createuserpopwin.text_mobile.gettext().tostring().trim(); string info1 = createuserpopwin.text_info.gettext().tostring().trim(); system.out.println(name1+"——"+mobile1+"——"+info1); createuserpopwin.dismiss(); break; } } };
以上就是本文的全部内容,希望对大家的学习android有所帮助。
上一篇: c# 快速排序算法