Android中的弹出框在被点击时, 无论点击哪个按钮都会关闭窗口。 但是有的情况下我们不需要立即关闭窗口。下面的情况是弹出框中要求用户输入文件名, 并在点击确定时检查文件名的合法性, 不合法则提示用户重新输入, 弹出框要保持在界面上。 点击取消时弹出框消失。 这里通过对控件的反射来实现:?
/**our button handler**/?
??? private ButtonHandler bHandler;?
//pop up dialog with own button handler?
??? private void popUpDialog(AlertDialog dialog) {?
??????? /*?
???????? * alert dialog's default handler will always close dialog whenever user?
???????? * clicks on which button. we have to replace default handler with our?
???????? * own handler for blocking close action.?
???????? * Reflection helps a lot.?
???????? */?
??????? try {?
??????????? Field field = dialog.getClass().getDeclaredField("mAlert");?
??????????? field.setAccessible(true);?
????????????
??????????? //retrieve mAlert value?
??????????? Object obj = field.get(dialog);?
??????????? field = obj.getClass().getDeclaredField("mHandler");?
??????????? field.setAccessible(true);?
??????????? //replace mHandler with our own handler?
??????????? field.set(obj, bHandler);?
??????? } catch (SecurityException e) {?
??????????? Debug.error(e.getMessage());?
??????? } catch (NoSuchFieldException e) {?
??????????? Debug.error(e.getMessage());?
??????? } catch (IllegalArgumentException e) {?
??????????? Debug.error(e.getMessage());?
??????? } catch (IllegalAccessException e) {?
??????????? Debug.error(e.getMessage());?
??????? }?
????????
??????? //we can show this dialog now.?
??????? dialog.show();?
??? }?
??? /*?
???? * define our own button handler, do not deal with dismiss message.?
???? */?
??? class? ButtonHandler? extends? Handler {?
???????? private? WeakReference<DialogInterface> mDialog;?
???????? public ButtonHandler(DialogInterface dialog) {?
??????????? mDialog = new WeakReference<DialogInterface>(dialog);?
???????? }?
???????? public void handleMessage(Message msg) {?
???????????? switch (msg.what) {?
???????????????? case DialogInterface.BUTTON_POSITIVE:?
???????????????? case DialogInterface.BUTTON_NEGATIVE:?
???????????????? case DialogInterface.BUTTON_NEUTRAL:?
??????????????????? ((DialogInterface.OnClickListener) msg.obj).onClick(mDialog?
??????????????????????????? .get(), msg.what);?
???????????????????? break ;?
??????????? }?
??????? }?
??? }?
然后创建一个AlertDialog时如下去调用:?
//create dialog?
??????????? mDialog = new AlertDialog.Builder(mContext)?
??????????? .setView(selectImageView)?
??????????? .setNegativeButton(android.R.string.cancel, mOnDialogClickListener)?
??????????? .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {?
??????????????? public void onClick(DialogInterface dialog, int arg1) {?
??????????????????? //check input file name?
??????????????????? if (isValidFileName(fileNameInput.getText().toString())) {?
??????????????????????? String fileName = fileNameInput.getText().toString().trim();?
??????????????????????? mediaList.get(0).setName(fileName);?
??????????????????????? saveMedia(mediaList);?
??????????????????????? dialog.dismiss();?
??????????????????? } else {?
??????????????????????? Toast.makeText(mContext, R.string.invalid_file_name, 1500).show();?
??????????????????? }?
????????????????????
??????????????? }?
??????????? }).create();?
mOnDialogClickListener用来处理关闭窗口事件:?
/*?
???? * cancel dialog listener?
???? */?
??? OnClickListener mOnDialogClickListener = new DialogInterface.OnClickListener() {?
??????? public void onClick(DialogInterface dialog, int buttonId) {?
??????????? switch (buttonId) {?
??????????????? case DialogInterface.BUTTON_NEGATIVE:?
??????????????????? dialog.dismiss();?
??????????????????? break;?
??????????????? case DialogInterface.BUTTON_POSITIVE:?
????????????????????
??????????????????? break;?
??????????? }?
????????????
??????? }?
??? };
详细解决方案
统制android弹出框不消失(用到反射的方法)
热度:72 发布时间:2016-05-01 15:26:11.0
相关解决方案
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android 大量数据按周分组,该如何解决
- android RadioButton如何设置默认选中
- ksoap2-android-这个包,连接webService怎么设置超时
- android 怎么重新设置锚点
- android UI界面设计解决方案
- android 图片对象获取的有关问题
- android 怎么调用淘宝支付宝接口
- Android 沿袭InputMethodService自定义输入法
- android 关于服务连接的疑义
- android 两个activity如何通信
- android 怎么实现对view的放大和缩小
- android 教程解决方法
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
- android UI线程与AsyncTask的有关问题
- android(java)中的java.net能不能和c#的system.net.sockets进行tcp通信,该如何解决
- android ListView 中的onItemClick Intent 没法跳转
- android(java) 中文乱码的有关问题
- c#c++,android,ios(iphone),php,java视屏课程 散分
- android Post文件到ASP.NET的有关问题,能收到参数收不到文件
- RIM 替 Android 开发者提供免费的 PlayBook!2月13日前
- android 动态设立控件高度
- Android test project 编译方法
- android -相机使用教程(1)解决方法