android源码解析--Dialog
在学习设计模式建造者模式时,发现AlertDialog和它的内部类Builder就是比较典型的建造者模式,所以先分析下基类Dialog,然后再看子类AlertDialog和它的内部类Builder。
按照惯例,先看下类说明:
/** * @see Activity#onPreparePanel(int, View, Menu) */ public boolean onPreparePanel(int featureId, View view, Menu menu) { if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { boolean goforit = onPrepareOptionsMenu(menu); return goforit && menu.hasVisibleItems(); } return true; }实现Window.Callback接口的onPreparePanel方法。每次当panel窗口显示前,都会调用该函数,返回true显示,false则不显示。
/** * It is usually safe to proxy this call to the owner activity's * {@link Activity#onCreateOptionsMenu(Menu)} if the client desires the same * menu for this Dialog. * * @see Activity#onCreateOptionsMenu(Menu) * @see #getOwnerActivity() */ public boolean onCreateOptionsMenu(Menu menu) { return true; }调用创建该Dialog的Activity的onCreateOptionMenu方法。/** * {@inheritDoc} * * Note that if you override this method you should always call through * to the superclass implementation by calling super.onActionModeStarted(mode). */ public void onActionModeStarted(ActionMode mode) { mActionMode = mode; } /** * {@inheritDoc} * * Note that if you override this method you should always call through * to the superclass implementation by calling super.onActionModeFinished(mode). */ public void onActionModeFinished(ActionMode mode) { if (mode == mActionMode) { mActionMode = null; } }注意,如果你重写这个方法,你一定要调用父类的super.onActionModeFinished(mode)。开始时候设置mode,结束时把mode设为空。
private static final class ListenersHandler extends Handler { private WeakReference<DialogInterface> mDialog; public ListenersHandler(Dialog dialog) { mDialog = new WeakReference<DialogInterface>(dialog); } @Override public void handleMessage(Message msg) { switch (msg.what) { case DISMISS: ((OnDismissListener) msg.obj).onDismiss(mDialog.get()); break; case CANCEL: ((OnCancelListener) msg.obj).onCancel(mDialog.get()); break; case SHOW: ((OnShowListener) msg.obj).onShow(mDialog.get()); break; } } }
ListenersHandler的定义。将近3点了,有点困,想睡了。。。
查看更多源码内容:Android源码解析!
- 1楼chuange501昨天 13:00
- 不错,学习了
- Re: aomandeshangxiao昨天 13:40
- 回复chuange501n呵呵,谢谢。。。