当前位置:操作系统 > 安卓/Android >>

android源码浅析--AlertController

在android源码解析--AlertDialog及AlertDialog.Builder这篇文章中,讲到在Builder中功能的实现主要是调用AlertController实现的,而该类是android内部类,在package com.android.internal.app包中,不能在Eclipse中通过ctrl键来跟踪源码,所以使用Source Insight软件打开该软件源码,查看一下。
 
跟以前一样,先看下AlertController类中的私有成员变量:
 
 
[java]  
private final Context mContext;  
    private final DialogInte易做图ce mDialogInte易做图ce;  
    private final Window mWindow;  
      
    private CharSequence mTitle;  
  
    private CharSequence mMessage;  
  
    private ListView mListView;  
      
    private View mView;  
  
    private int mViewSpacingLeft;  
      
    private int mViewSpacingTop;  
      
    private int mViewSpacingRight;  
      
    private int mViewSpacingBottom;  
      
    private boolean mViewSpacingSpecified = false;  
      
    private Button mButtonPositive;  
  
    private CharSequence mButtonPositiveText;  
  
    private Message mButtonPositiveMessage;  
  
    private Button mButtonNegative;  
  
    private CharSequence mButtonNegativeText;  
  
    private Message mButtonNegativeMessage;  
  
    private Button mButtonNeutral;  
  
    private CharSequence mButtonNeutralText;  
  
    private Message mButtonNeutralMessage;  
  
    private ScrollView mScrollView;  
      
    private int mIconId = -1;  
      
    private Drawable mIcon;  
      
    private ImageView mIconView;  
      
    private TextView mTitleView;  
  
    private TextView mMessageView;  
  
    private View mCustomTitleView;  
      
    private boolean mForceInverseBackground;  
      
    private ListAdapter mAdapter;  
      
    private int mCheckedItem = -1;  
  
    private int mAlertDialogLayout;  
    private int mListLayout;  
    private int mMultiChoiceItemLayout;  
    private int mSingleChoiceItemLayout;  
    private int mListItemLayout;  
  
    private Handler mHandler;  
  mAlertDialogLayout:AlertDialog布局
  mListLayout:List布局
  mMultiChoiceItemLayout:多选布局
  mSingleChoiceItemLayout:单选布局
mListItemLayout:listItem布局
 
 
 
接着下面是一个自定义的View OnClickListener事件,其目的把点击对象的信息发送到对应的线程(UI线程):
 
 
[java]  
View.OnClickListener mButtonHandler = new View.OnClickListener() {  
        public void onClick(View v) {  
            Message m = null;  
            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);  
            }  
            if (m != null) {  
                m.sendToTarget();  
            }  
  
            // Post a message so we dismiss after the above handlers are executed  
            mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialogInte易做图ce)  
                    .sendToTarget();  
        }  
    };  
 
前面获取点击传递的Message,发送到目标线程(UI线程),然后再发送一个Message,通知UI线程关闭此对话框。里面使用到的ButtonHandler.MSG_DISMISS_DIALOG,就在下面代码中定义(关于Message和Handler发送消息,请参看前面博文)。
 
 
 
[java]  
private static final class ButtonHandler extends Handler {  
        // Button clicks have Message.what as the BUTTON{1,2,3} constant  
        private static final int MSG_DISMISS_DIALOG = 1;  
          
        private WeakReference<DialogInte易做图ce> mDialog;  
  
        public ButtonHandler(DialogInte易做图ce dialog) {  
            mDialog = new WeakReference<DialogInte易做图ce>(dialog);  
        }  
  
        @Override  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
                  
                case DialogInte易做图ce.BUTTON_POSITIVE:  
                case DialogInte易做图ce.BUTTON_NEGATIVE:  
                case DialogInte易做图ce.BUTTON_NEUTRAL:  
                    ((DialogInte易做图ce.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);  
                    break;  
                     &nbs
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,