主页 > 未分类 > 安卓AlertActivity setCancelable方法的替代实现

安卓AlertActivity setCancelable方法的替代实现

使用AlertActivity会弹出一个对话框,而这个对话框时封装好的,并且没有定义是否允许设置点击对话框外部取消。不能简单的通过继承AlertController.AlertParams和AlertController来实现,因为其中的mWindow为私有的方法,即使解决了私有问题,也不能将AlertActivity中的AlertController.AlertParams简单替换为继承后的类。

解决方法为在AlertActivity的子类里调用Window的setCloseOnTouchOutside(boolean)方法实现:

getWindow().setCloseOnTouchOutside(false);

AlertController中的的apply方法如下所示,其中对setCancelable并没有实现,可见并没有提供该接口:

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 (mIconAttrId > 0) {
      dialog.setIcon(dialog.getIconAttributeResId(mIconAttrId));
    }
 }
 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);
  }
 */

}

发表评论

邮箱地址不会被公开。 必填项已用*标注