Android:在AlertDialog点击按钮隐藏对话框
来源:互联网 发布:ps mac 破解版 编辑:程序博客网 时间:2024/04/30 07:13
前言
最近在优化一个dialog,里面使用AndroidTreeView展示树状数据。由于数据量颇多,每次打开dialog都会卡顿一两秒,用户体验不好。于是想第一次加载完dialog后,缓存dialog,再打开时就不用重新生成了。
开始以为很简单,结果也花了点时间。到底实现功能了,在这里记录一下。
Dialog点击按钮隐藏
创建Dialog使用的是DialogFragment,官方不推荐直接使用Dialog创建对话框,具体使用方法就不说了。
使用AlertDialog.Builder的setXXXButton方法,就可以设置Positive、Negative、Neutral三种按钮的onClick动作,代码如下:
builder.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } } });
目标是隐藏Dialog,理论上在onClick方法里调用Dialog的hide()方法就可以了。实际没有这么简单,onClick方法调用完毕后,会自动调用Dialog的dismiss方法,导致Dialog被销毁,无法实现隐藏。
从setPositiveButton()开始跟踪源码,方法会为AlertDialog.Builder的AlertController.AlertParams设置参数,记录OnClickListener方法:
public Builder setPositiveButton(int textId, final OnClickListener listener) { P.mPositiveButtonText = P.mContext.getText(textId); P.mPositiveButtonListener = listener; return this; }
Dialog创建的时候,会设置按钮的listener,最终由mButtonHandler处理事件。从最后一句代码可以知道,Dialog会自动调用dismiss方法关闭:
private final View.OnClickListener mButtonHandler = new View.OnClickListener() { @Override public void onClick(View v) { final Message m; if (v == mButtonPositive && mButtonPositiveMessage != null) { m = Message.obtain(mButtonPositiveMessage); } else if (v == mButtonNegative && mButtonNegativeMessage != null) { m = Message.obtain(mButtonNegativeMessage); } else if (v == mButtonNeutral && mButtonNeutralMessage != null) { m = Message.obtain(mButtonNeutralMessage); } else { m = null; } if (m != null) { m.sendToTarget(); } // Post a message so we dismiss after the above handlers are executed mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialog) .sendToTarget(); } };
@Override public void dismiss() { if (Looper.myLooper() == mHandler.getLooper()) { dismissDialog(); } else { mHandler.post(mDismissAction); } } void dismissDialog() { if (mDecor == null || !mShowing) { return; } if (mWindow.isDestroyed()) { Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!"); return; } try { mWindowManager.removeViewImmediate(mDecor); } finally { if (mActionMode != null) { mActionMode.finish(); } mDecor = null; mWindow.closeAllPanels(); onStop(); mShowing = false; sendDismissMessage(); } }
参考别人的解决方法,想要阻止dialog自动关闭,只要将mShowing设置为false即可。需要使用反射,代码如下:
try { Field field = dialog.getClass().getSuperclass().getSuperclass().getDeclaredField("mShowing"); field.setAccessible(true); field.set(dialog, false);} catch (Exception e) { e.printStackTrace();}
最后实现Dialog点击按钮隐藏可以如下代码所写:
builder.setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { try { Field field = dialog.getClass().getSuperclass().getSuperclass().getDeclaredField("mShowing"); field.setAccessible(true); field.set(dialog, false); } catch (Exception e) { e.printStackTrace(); } mAlertDialog.hide(); } });
Dialog的hide()很简单,只是将View设置为GONE:
public void hide() { if (mDecor != null) { mDecor.setVisibility(View.GONE); }}
Dialog隐藏后显示
Dialog的重新显示比较简单,只要调用show()方法即可,但要注意,mShowing在Dialog隐藏的时候已经被设置为false,查看show()的代码如下:
public void show() { if (mShowing) { if (mDecor != null) { if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) { mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR); } mDecor.setVisibility(View.VISIBLE); } return; } //省略其他代码 }
想要被缓存Dialog正常地显示出来,需要将mShowing重新设置为true,才能调用到Decor.setVisibility(View.VISIBLE);
try { Field field = alertDialog.getClass().getSuperclass().getSuperclass().getDeclaredField("mShowing"); field.setAccessible(true); field.set(alertDialog, true);} catch (Exception e) { e.printStackTrace();}dialogFragment.getDialog().show();
待研究问题
当页面跳转不需要Dialog时,需要记得主动销毁。我是在Activity中缓存DialogFragment对象,一开始在onDestory时调用dismiss,但应用收起后台再返回前台会报错,在onStop时调用也不行,只有在onPause时才行。我估计在onStop时会做一些缓存操作,而手动缓存的Fragment影响了系统的操作。
知道了一些,但又知道我不知道很多。对于Activity的生命周期理解还不够,接下来要重点研究。
后记
第一次发表博客,感觉不错。接下来,在工作中遇到的问题和新学习的知识,都要尽量记录,每个星期发一篇!如果亲爱的你有任何建议,将告诉我,谢谢。
- Android:在AlertDialog点击按钮隐藏对话框
- 【Android】AlertDialog点击按钮后不自动关闭对话框
- Android设置AlertDialog点击按钮对话框不关闭(转)
- Android:实现点击AlertDialog上按钮时不关闭对话框
- Android:实现点击AlertDialog上按钮时不关闭对话框
- 点击AlertDialog按钮不关闭对话框
- Android Alertdialog对话框点击消失?
- Android下关于AlertDialog点击 确定或者取消按钮 对话框自动消失的 解决办法
- Android中让AlertDialog点击了按钮后对话框不消失
- Android 点击AlertDialog上的确定和取消按钮,使对话框不消失
- Android实现点击AlertDialog上按钮时不关闭对话框的方法
- Android实现点击AlertDialog上按钮时不关闭对话框的方法
- Android实现点击AlertDialog上按钮时不关闭对话框的方法
- Android Studio AlertDialog 为对话框添加按钮
- Android中Alertdialog对话框点击消失?
- Android中 Alertdialog对话框点击消失?
- android alertDialog 点击按钮后保留dialog
- 点击AlertDialog的按钮时,不自动关闭对话框
- HttpClient在 AndroidStudio中使用
- Python 中的进程、线程、协程、同步、异步、回调
- Cocos2d-x 读取网络图片创建精灵
- 从头到尾彻底解析Hash表算法
- [转载]cocos2d-x学习笔记3:更改HelloWorld,建立自己的小项目
- Android:在AlertDialog点击按钮隐藏对话框
- 百度云推送
- 在CTU上同步CST时间
- NoSQL 数据库分类
- Java中构造函数重载和方法重载
- .NET后台confirm对话框以及JS调用后台相关方法
- java零基础入门笔记第三天
- Android 使用 TableLayout 布局拉伸宽度 平均分布各列
- Android tools:context=".MainActivity"作用