android源码浅析--AlertController在android源码解析--AlertDialog及AlertDialog.Builder这篇文章中,讲到
android源码浅析--AlertController
在android源码解析--AlertDialog及AlertDialog.Builder这篇文章中,讲到在Builder中功能的实现主要是调用AlertController实现的,而该类是android内部类,在package com.android.internal.app包中,不能在Eclipse中通过ctrl键来跟踪源码,所以使用Source Insight软件打开该软件源码,查看一下。
跟以前一样,先看下AlertController类中的私有成员变量:
设置对话框后面的窗体是否能够获得焦点(能不能响应用户操作触发的事件):
下面定义了一个静态内部类:RecycleListView(此listView Measure状态下回收视图)public void apply(AlertController dialog) { if (mCustomTitleView != null) { dialog.setCustomTitle(mCustomTitleView); } else { if (mTitle != null) { dialog.setTitle(mTitle); } if (mIcon != null) { dialog.setIcon(mIcon); } if (mIconId >= 0) { dialog.setIcon(mIconId); } } if (mMessage != null) { dialog.setMessage(mMessage); } if (mPositiveButtonText != null) { dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText, mPositiveButtonListener, null); } if (mNegativeButtonText != null) { dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText, mNegativeButtonListener, null); } if (mNeutralButtonText != null) { dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText, mNeutralButtonListener, null); } if (mForceInverseBackground) { dialog.setInverseBackgroundForced(true); } // For a list, the client can either supply an array of items or an // adapter or a cursor if ((mItems != null) || (mCursor != null) || (mAdapter != null)) { createListView(dialog); } if (mView != null) { if (mViewSpacingSpecified) { dialog.setView(mView, mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight, mViewSpacingBottom); } else { dialog.setView(mView); } } /* dialog.setCancelable(mCancelable); dialog.setOnCancelListener(mOnCancelListener); if (mOnKeyListener != null) { dialog.setOnKeyListener(mOnKeyListener); } */ }
为Dialog设置各种属性,在AlertDialog.Builder的create()方法中调用了该方法。
为Dialog创建一个listView:
private void createListView(final AlertController dialog) { final RecycleListView listView = (RecycleListView) mInflater.inflate(dialog.mListLayout, null); ListAdapter adapter; //是否是多选 if (mIsMultiChoice) { //是否是从数据库中取出的值 if (mCursor == null) { adapter = new ArrayAdapter<CharSequence>( mContext, dialog.mMultiChoiceItemLayout, R.id.text1, mItems) { @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); if (mCheckedItems != null) { boolean isItemChecked = mCheckedItems[position]; if (isItemChecked) { listView.setItemChecked(position, true); } } return view; } }; } else { adapter = new CursorAdapter(mContext, mCursor, false) { private final int mLabelIndex; private final int mIsCheckedIndex; { final Cursor cursor = getCursor(); mLabelIndex = cursor.getColumnIndexOrThrow(mLabelColumn); mIsCheckedIndex = cursor.getColumnIndexOrThrow(mIsCheckedColumn); } @Override public void bindView(View view, Context context, Cursor cursor) { CheckedTextView text = (CheckedTextView) view.findViewById(R.id.text1); text.setText(cursor.getString(mLabelIndex)); listView.setItemChecked(cursor.getPosition(), cursor.getInt(mIsCheckedIndex) == 1); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return mInflater.inflate(dialog.mMultiChoiceItemLayout, parent, false); } }; } } else {//如果是单选或者普通的listview int layout = mIsSingleChoice ? dialog.mSingleChoiceItemLayout : dialog.mListItemLayout; if (mCursor == null) { adapter = (mAdapter != null) ? mAdapter : new ArrayAdapter<CharSequence>(mContext, layout, R.id.text1, mItems); } else { adapter = new SimpleCursorAdapter(mContext, layout, mCursor, new String[]{mLabelColumn}, new int[]{R.id.text1}); } } if (mOnPrepareListViewListener != null) { mOnPrepareListViewListener.onPrepareListView(listView); } /* Don't directly set the adapter on the ListView as we might * want to add a footer to the ListView later. * 不要直接为listView设置adapter,因为一会可能需要为listView设置页脚 */ dialog.mAdapter = adapter; dialog.mCheckedItem = mCheckedItem; //设置监听 if (mOnClickListener != null) { listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { mOnClickListener.onClick(dialog.mDialogInterface, position); if (!mIsSingleChoice) { dialog.mDialogInterface.dismiss(); } } }); } else if (mOnCheckboxClickListener != null) { //多选监听 listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { if (mCheckedItems != null) { mCheckedItems[position] = listView.isItemChecked(position); } mOnCheckboxClickListener.onClick( dialog.mDialogInterface, position, listView.isItemChecked(position)); } }); } // Attach a given OnItemSelectedListener to the ListView if (mOnItemSelectedListener != null) { listView.setOnItemSelectedListener(mOnItemSelectedListener); } //如有选择项,选择单选或者多选 if (mIsSingleChoice) { listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } else if (mIsMultiChoice) { listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); } listView.mRecycleOnMeasure = mRecycleOnMeasure; dialog.mListView = listView; } }
这个方法逻辑注释在代码中,不详细解释了,都比较简单。看AlertController源码,可以结合AlertDialog源码看。因为在AlertDialog中的大部分功能实现是靠调用AlertController类来实现。
更多源码解析:Android源码解析
PS:终于简单的把这篇源码看完,距离AlertDialog已经有一个月了,时间飞快,多需努力,我想回家。
