您的位置:首页 > 其它

屏蔽Dialog与其子类 Home键(4.0以下)

2012-11-29 12:57 176 查看
在Dialog 方法show()之后再调用dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);方法能实现禁止Home键。

改写后的AlertDialog:

package android.mis.securitymanager.camera;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.mis.securitymanager.R;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MAlertDialog extends AlertDialog{

	protected MAlertDialog(Context context) {
		super(context);
	}
	@Override
	public void onAttachedToWindow() {
		getWindow().setType(LayoutParams.TYPE_KEYGUARD_DIALOG);
		super.onAttachedToWindow();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_HOME)
			return true;
		return super.onKeyDown(keyCode, event);
	}
	 public static class Builder {
	        private Parameter p;
	        
	        /**
	         * Constructor using a context for this builder and the {@link AlertDialog} it creates.
	         */
	        public Builder(Context context) {
	        	p = new Parameter();
	        	p.context = context;
	        }
	        
	        /**
	         * Set the title using the given resource id.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setTitle(int titleId) {
	            p.title = p.context.getString(titleId);
	            return this;
	        }
	        
	        /**
	         * Set the title displayed in the {@link Dialog}.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setTitle(CharSequence title) {
	           p.title = title;
	            return this;
	        }
	
	        
	        /**
	         * Set the message to display using the given resource id.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setMessage(int messageId) {
	            p.message = p.context.getString(messageId);
	            return this;
	        }
	        
	        /**
	         * Set the message to display.
	          *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setMessage(CharSequence message) {
	            p.message = message+"";
	            return this;
	        }
	        
	        /**
	         * Set the resource id of the {@link Drawable} to be used in the title.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setIcon(int iconId) {
	           p.icon = p.context.getResources().getDrawable(iconId);
	            return this;
	        }
	        
	        /**
	         * Set the {@link Drawable} to be used in the title.
	          *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setIcon(Drawable icon) {
	            p.icon = icon;
	            return this;
	        }
	        
	        /**
	         * Set a listener to be invoked when the positive button of the dialog is pressed.
	         * @param textId The resource id of the text to display in the positive button
	         * @param listener The {@link DialogInterface.OnClickListener} to use.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setPositiveButton(int textId, final OnClickListener listener) {
	        	p.positiveText = p.context.getString(textId);
	        	p.pListener = listener;
	            return this;
	        }
	        
	        /**
	         * Set a listener to be invoked when the positive button of the dialog is pressed.
	         * @param text The text to display in the positive button
	         * @param listener The {@link DialogInterface.OnClickListener} to use.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
	        	p.positiveText = text+"";
	        	p.pListener = listener;
	            return this;
	        }
	        
	        /**
	         * Set a listener to be invoked when the negative button of the dialog is pressed.
	         * @param textId The resource id of the text to display in the negative button
	         * @param listener The {@link DialogInterface.OnClickListener} to use.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setNegativeButton(int textId, final OnClickListener listener) {
	        	p.negativiText = p.context.getString(textId);
	        	p.nListener = listener;
	            return this;
	        }
	        
	        /**
	         * Set a listener to be invoked when the negative button of the dialog is pressed.
	         * @param text The text to display in the negative button
	         * @param listener The {@link DialogInterface.OnClickListener} to use.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
	        	p.negativiText = text+"";
	        	p.nListener = listener;
	            return this;
	        }
	        
	        /**
	         * Set a listener to be invoked when the neutral button of the dialog is pressed.
	         * @param textId The resource id of the text to display in the neutral button
	         * @param listener The {@link DialogInterface.OnClickListener} to use.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
//	        public Builder setNeutralButton(int textId, final OnClickListener listener) {
//	            P.mNeutralButtonText = P.mContext.getText(textId);
//	            P.mNeutralButtonListener = listener;
//	            return this;
//	        }
//	        
//	        /**
//	         * Set a listener to be invoked when the neutral button of the dialog is pressed.
//	         * @param text The text to display in the neutral button
//	         * @param listener The {@link DialogInterface.OnClickListener} to use.
//	         *
//	         * @return This Builder object to allow for chaining of calls to set methods
//	         */
//	        public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {
//	            P.mNeutralButtonText = text;
//	            P.mNeutralButtonListener = listener;
//	            return this;
//	        }
	        
	        /**
	         * Sets whether the dialog is cancelable or not default is true.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setCancelable(boolean cancelable) {
	            p.mCancelable = cancelable;
	            return this;
	        }
	        
	        /**
	         * Sets the callback that will be called if the dialog is canceled.
	         * @see #setCancelable(boolean)
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
//	        public Builder setOnCancelListener(OnCancelListener onCancelListener) {
//	            md.setOnCancelListener(onCancelListener);
//	            return this;
//	        }
//	        
	        /**
	         * Sets the callback that will be called if a key is dispatched to the dialog.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setOnKeyListener(OnKeyListener onKeyListener) {
	            md.setOnKeyListener(onKeyListener);
	            return this;
	        }*/
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content, you will be notified of the
	         * selected item via the supplied listener. This should be an array type i.e. R.array.foo
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setItems(int itemsId, final OnClickListener listener) {
	            P.mItems = P.mContext.getResources().getTextArray(itemsId);
	            P.mOnClickListener = listener;
	            return this;
	        }*/
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content, you will be notified of the
	         * selected item via the supplied listener.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setItems(String[] items, final OnClickListener listener) {
	            p.mItems = items;
	            p.mOnClickListener = listener;
	            p.mIsItemChoice = true;
	            return this;
	        }
	        
	        /**
	         * Set a list of items, which are supplied by the given {@link ListAdapter}, to be
	         * displayed in the dialog as the content, you will be notified of the
	         * selected item via the supplied listener.
	         * 
	         * @param adapter The {@link ListAdapter} to supply the list of items
	         * @param listener The listener that will be called when an item is clicked.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
	            P.mAdapter = adapter;
	            P.mOnClickListener = listener;
	            return this;
	        }*/
	        
	        /**
	         * Set a list of items, which are supplied by the given {@link Cursor}, to be
	         * displayed in the dialog as the content, you will be notified of the
	         * selected item via the supplied listener.
	         * 
	         * @param cursor The {@link Cursor} to supply the list of items
	         * @param listener The listener that will be called when an item is clicked.
	         * @param labelColumn The column name on the cursor containing the string to display
	         *          in the label.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setCursor(final Cursor cursor, final OnClickListener listener,
	                String labelColumn) {
	            P.mCursor = cursor;
	            P.mLabelColumn = labelColumn;
	            P.mOnClickListener = listener;
	            return this;
	        }*/
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content,
	         * you will be notified of the selected item via the supplied listener.
	         * This should be an array type, e.g. R.array.foo. The list will have
	         * a check mark displayed to the right of the text for each checked
	         * item. Clicking on an item in the list will not dismiss the dialog.
	         * Clicking on a button will dismiss the dialog.
	         * 
	         * @param itemsId the resource id of an array i.e. R.array.foo
	         * @param checkedItems specifies which items are checked. It should be null in which case no
	         *        items are checked. If non null it must be exactly the same length as the array of
	         *        items.
	         * @param listener notified when an item on the list is clicked. The dialog will not be
	         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
	         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems, 
	                final OnMultiChoiceClickListener listener) {
	            P.mItems = P.mContext.getResources().getTextArray(itemsId);
	            P.mOnCheckboxClickListener = listener;
	            P.mCheckedItems = checkedItems;
	            P.mIsMultiChoice = true;
	            return this;
	        }*/
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content,
	         * you will be notified of the selected item via the supplied listener.
	         * The list will have a check mark displayed to the right of the text
	         * for each checked item. Clicking on an item in the list will not
	         * dismiss the dialog. Clicking on a button will dismiss the dialog.
	         * 
	         * @param items the text of the items to be displayed in the list.
	         * @param checkedItems specifies which items are checked. It should be null in which case no
	         *        items are checked. If non null it must be exactly the same length as the array of
	         *        items.
	         * @param listener notified when an item on the list is clicked. The dialog will not be
	         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
	         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
	        public Builder setMultiChoiceItems(String[] items, boolean[] checkedItems, 
	                final OnMultiChoiceClickListener listener) {
	            p.mItems = items;
	            p.multiChoiceListener = listener;
	            p.mCheckedItems = checkedItems;
	            p.mIsMultiChoice = true;
	            return this;
	        }
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content,
	         * you will be notified of the selected item via the supplied listener.
	         * The list will have a check mark displayed to the right of the text
	         * for each checked item. Clicking on an item in the list will not
	         * dismiss the dialog. Clicking on a button will dismiss the dialog.
	         * 
	         * @param cursor the cursor used to provide the items.
	         * @param isCheckedColumn specifies the column name on the cursor to use to determine
	         *        whether a checkbox is checked or not. It must return an integer value where 1
	         *        means checked and 0 means unchecked.
	         * @param labelColumn The column name on the cursor containing the string to display in the
	         *        label.
	         * @param listener notified when an item on the list is clicked. The dialog will not be
	         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
	         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, 
	                final OnMultiChoiceClickListener listener) {
	            P.mCursor = cursor;
	            P.mOnCheckboxClickListener = listener;
	            P.mIsCheckedColumn = isCheckedColumn;
	            P.mLabelColumn = labelColumn;
	            P.mIsMultiChoice = true;
	            return this;
	        }*/
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content, you will be notified of
	         * the selected item via the supplied listener. This should be an array type i.e.
	         * R.array.foo The list will have a check mark displayed to the right of the text for the
	         * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a
	         * button will dismiss the dialog.
	         * 
	         * @param itemsId the resource id of an array i.e. R.array.foo
	         * @param checkedItem specifies which item is checked. If -1 no items are checked.
	         * @param listener notified when an item on the list is clicked. The dialog will not be
	         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
	         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setSingleChoiceItems(int itemsId, int checkedItem, 
	                final OnClickListener listener) {
	            P.mItems = P.mContext.getResources().getTextArray(itemsId);
	            P.mOnClickListener = listener;
	            P.mCheckedItem = checkedItem;
	            P.mIsSingleChoice = true;
	            return this;
	        }*/
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content, you will be notified of
	         * the selected item via the supplied listener. The list will have a check mark displayed to
	         * the right of the text for the checked item. Clicking on an item in the list will not
	         * dismiss the dialog. Clicking on a button will dismiss the dialog.
	         * 
	         * @param cursor the cursor to retrieve the items from.
	         * @param checkedItem specifies which item is checked. If -1 no items are checked.
	         * @param labelColumn The column name on the cursor containing the string to display in the
	         *        label.
	         * @param listener notified when an item on the list is clicked. The dialog will not be
	         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
	         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, 
	                final OnClickListener listener) {
	            P.mCursor = cursor;
	            P.mOnClickListener = listener;
	            P.mCheckedItem = checkedItem;
	            P.mLabelColumn = labelColumn;
	            P.mIsSingleChoice = true;
	            return this;
	        }*/
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content, you will be notified of
	         * the selected item via the supplied listener. The list will have a check mark displayed to
	         * the right of the text for the checked item. Clicking on an item in the list will not
	         * dismiss the dialog. Clicking on a button will dismiss the dialog.
	         * 
	         * @param items the items to be displayed.
	         * @param checkedItem specifies which item is checked. If -1 no items are checked.
	         * @param listener notified when an item on the list is clicked. The dialog will not be
	         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
	         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
	            P.mItems = items;
	            P.mOnClickListener = listener;
	            P.mCheckedItem = checkedItem;
	            P.mIsSingleChoice = true;
	            return this;
	        } */
	        
	        /**
	         * Set a list of items to be displayed in the dialog as the content, you will be notified of
	         * the selected item via the supplied listener. The list will have a check mark displayed to
	         * the right of the text for the checked item. Clicking on an item in the list will not
	         * dismiss the dialog. Clicking on a button will dismiss the dialog.
	         * 
	         * @param adapter The {@link ListAdapter} to supply the list of items
	         * @param checkedItem specifies which item is checked. If -1 no items are checked.
	         * @param listener notified when an item on the list is clicked. The dialog will not be
	         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
	         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {
	            P.mAdapter = adapter;
	            P.mOnClickListener = listener;
	            P.mCheckedItem = checkedItem;
	            P.mIsSingleChoice = true;
	            return this;
	        }*/
	        
	        /**
	         * Sets a listener to be invoked when an item in the list is selected.
	         * 
	         * @param listener The listener to be invoked.
	         * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)
	         *
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
	            P.mOnItemSelectedListener = listener;
	            return this;
	        }*/
	        
	        /**
	         * Set a custom view to be the contents of the Dialog. If the supplied view is an instance
	         * of a {@link ListView} the light background will be used.
	         *
	         * @param view The view to use as the contents of the Dialog.
	         * 
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setView(View view) {
	            md.setView(view);
	            return this;
	        }*/
	        
	        /**
	         * Set a custom view to be the contents of the Dialog, specifying the
	         * spacing to appear around that view. If the supplied view is an
	         * instance of a {@link ListView} the light background will be used.
	         * 
	         * @param view The view to use as the contents of the Dialog.
	         * @param viewSpacingLeft Spacing between the left edge of the view and
	         *        the dialog frame
	         * @param viewSpacingTop Spacing between the top edge of the view and
	         *        the dialog frame
	         * @param viewSpacingRight Spacing between the right edge of the view
	         *        and the dialog frame
	         * @param viewSpacingBottom Spacing between the bottom edge of the view
	         *        and the dialog frame
	         * @return This Builder object to allow for chaining of calls to set
	         *         methods
	         *         
	         * 
	         * This is currently hidden because it seems like people should just
	         * be able to put padding around the view.
	         * @hide
	         */
/*	        public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
	                int viewSpacingRight, int viewSpacingBottom) {
	            P.mView = view;
	            P.mViewSpacingSpecified = true;
	            P.mViewSpacingLeft = viewSpacingLeft;
	            P.mViewSpacingTop = viewSpacingTop;
	            P.mViewSpacingRight = viewSpacingRight;
	            P.mViewSpacingBottom = viewSpacingBottom;
	            return this;
	        }*/
	        
	        /**
	         * Sets the Dialog to use the inverse background, regardless of what the
	         * contents is.
	         * 
	         * @param useInverseBackground Whether to use the inverse background
	         * 
	         * @return This Builder object to allow for chaining of calls to set methods
	         */
/*	        public Builder setInverseBackgroundForced(boolean useInverseBackground) {
	            P.mForceInverseBackground = useInverseBackground;
	            return this;
	        }*/

	        /**
	         * @hide
	         */
/*	        public Builder setRecycleOnMeasureEnabled(boolean enabled) {
	            P.mRecycleOnMeasure = enabled;
	            return this;
	        }*/

	        /**
	         * Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not
	         * {@link Dialog#show()} the dialog. This allows the user to do any extra processing
	         * before displaying the dialog. Use {@link #show()} if you don't have any other processing
	         * to do and want this to be created and displayed.
	         */
	        public MAlertDialog create() {
	            final MAlertDialog dialog = new MAlertDialog(p.context);
	            p.apply(dialog);
	            dialog.setCancelable(p.mCancelable);

	            return dialog;
	        }

	        /**
	         * Creates a {@link AlertDialog} with the arguments supplied to this builder and
	         * {@link Dialog#show()}'s the dialog.
	         */
	        public AlertDialog show() {
	            MAlertDialog dialog = create();
	            dialog.show();
	            return dialog;
	        }
	    }
		static class Parameter {
			public OnMultiChoiceClickListener multiChoiceListener;
			CharSequence title;
			String[] mItems;
			boolean[] mCheckedItems;
			boolean mIsMultiChoice;
			boolean mIsItemChoice;
			String positiveText;
			String negativiText;
			String message;
			Context context;
			Drawable icon;
			Dialog.OnClickListener mOnClickListener;
			Dialog.OnClickListener pListener;
			Dialog.OnClickListener nListener;
			boolean mCancelable = true;
			
			public void apply(final MAlertDialog sd) {
				if (title != null)
					sd.setTitle(title);
				if(icon!=null)
					sd.setIcon(icon);
				if (mIsMultiChoice) {
					ListView lv = new ListView(context);;
					ArrayAdapter<String> aa = new ArrayAdapter<String>(context,
							android.R.layout.simple_list_item_multiple_choice, mItems);
					lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
					sd.setView(lv);
					lv.setAdapter(aa);
					if(multiChoiceListener != null)
					lv.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> parent, View view,
								int position, long id) {
                                                               sd.dismiss();
							CheckedTextView text = (CheckedTextView) view
									.findViewById(android.R.id.text1);
							if (text.isChecked() == false)
								multiChoiceListener.onClick(sd, position, true);
							else
								multiChoiceListener.onClick(sd, position, false);
						}
					});
					if (mCheckedItems != null) {
						int len = mItems.length > mCheckedItems.length ? mCheckedItems.length
								: mItems.length;
						for (int i = 0; i < len; i++)
							lv.setItemChecked(i, mCheckedItems[i]);
					}
				}else if(mIsItemChoice){
					ListView lv = new ListView(context);;
					ArrayAdapter<String> aa = new ArrayAdapter<String>(context,
							R.layout.simple_list_item_1, mItems);
					
					sd.setView(lv);
					lv.setAdapter(aa);
					lv.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
                                                            sd.dismiss();
							if(mOnClickListener != null)
								mOnClickListener.onClick(sd, position);
						}
					});
				}else if(message!=null)
					sd.setMessage(message);
				if(positiveText != null)
					sd.setButton(AlertDialog.BUTTON_POSITIVE, positiveText, pListener);
				if(negativiText != null)
					sd.setButton(AlertDialog.BUTTON_NEGATIVE, negativiText, nListener);
				
			}
		}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: