AlertDialog 处理方法二

使用Java反射机制替换AlertConter中的mHandler, 实现自处理"Done" & "Cancel"按钮.

Android中的弹出框在被点击时, 无论点击哪个按钮都会关闭窗口。 但是有的情况下我们不需要立即关闭窗口。下面的情况是弹出框中要求用户输入文件名, 并在点击确定时检查文件名的合法性, 不合法则提示用户重新输入, 弹出框要保持在界面上。 点击取消时弹出框消失。 这里通过对控件的反射来实现:

package com.example.test;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DialogActivity extends Activity {
	private String TAG = "DialogActivity";

	private AlertDialog mAlert = null;
	private Button btn = null;
	/*
	 * cancel dialog listener
	 */
	android.content.DialogInterface.OnClickListener mOnDialogClickListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int buttonId) {
			switch (buttonId) {
			case DialogInterface.BUTTON_NEGATIVE:
				Log.v(TAG, " 222222222");
				dialog.dismiss();
				break;
			case DialogInterface.BUTTON_POSITIVE:
				Log.v(TAG, " 111111111");
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		this.setContentView(R.layout.main);
		
		btn = (Button)findViewById(R.id.button1);
		btn.setOnClickListener( new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(0);
				bHandler = new ButtonHandler(mAlert);
				popUpDialog(mAlert);
			}
		});
		super.onCreate(savedInstanceState);
	}
	
	

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		LayoutInflater factory = LayoutInflater.from(this);
		View selectImageView = factory.inflate(R.layout.alter_dialog_text_entry, null);	
		
		mAlert = new AlertDialog.Builder(this).setView(selectImageView)
				.setNegativeButton(android.R.string.cancel,
						mOnDialogClickListener).setPositiveButton(
						android.R.string.ok,
						mOnDialogClickListener).create();

		return mAlert;
	}

	/** our button handler **/
	private ButtonHandler bHandler;

	// pop up dialog with own button handler
	private void popUpDialog(AlertDialog dialog) {
		
		Log.v(TAG, " 33333333333");
		/*
		 * alert dialog's default handler will always close dialog whenever user
		 * clicks on which button. we have to replace default handler with our
		 * own handler for blocking close action. Reflection helps a lot.
		 */
		try {
			Field field = dialog.getClass().getDeclaredField("mAlert");
			field.setAccessible(true);

			// retrieve mAlert value
			Object obj = field.get(dialog);
			field = obj.getClass().getDeclaredField("mHandler");
			field.setAccessible(true);
			// replace mHandler with our own handler
			field.set(obj, bHandler);
		} catch (SecurityException e) {
			e.printStackTrace();
			// Debug.error(e.getMessage());
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
			// Debug.error(e.getMessage());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			// Debug.error(e.getMessage());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			// Debug.error(e.getMessage());
		}
		dialog.show();
	}

	/*
	 * define our own button handler, do not deal with dismiss message.
	 */
	class ButtonHandler extends Handler {

		private WeakReference<DialogInterface> mDialog;

		public ButtonHandler(DialogInterface dialog) {
			mDialog = new WeakReference<DialogInterface>(dialog);
		}

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DialogInterface.BUTTON_POSITIVE:
			case DialogInterface.BUTTON_NEGATIVE:
			case DialogInterface.BUTTON_NEUTRAL:
				((DialogInterface.OnClickListener) msg.obj).onClick(mDialog
						.get(), msg.what);
				Log.v(TAG, " 44444444444444444444");
				break;
			}
		}
	}
}	


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值