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;
}
}
}
}
相關文章
- Android彈窗二則: PopupWindow和AlertDialogAndroid
- Android彈窗二則:PopupWindow和AlertDialogAndroid
- [特徵工程系列二]顯性特徵的基本處理方法特徵工程
- Python錯誤處理和異常處理(二)Python
- Python資料處理(二):處理 Excel 資料PythonExcel
- Windows批處理學習(二)——批處理(3)薦Windows
- 非同步處理方法非同步
- 影像預處理方法
- APM RUEI processor處理程式hang死處理方法
- 專案常用JS方法封裝(二) [ 時間相關處理 ]JS封裝
- Python-OpenCV 處理視訊(二): 視訊處理PythonOpenCV
- 二進位制處理技巧
- Spring (二) 事務處理Spring
- 形態學影像處理(二)
- javaScript事件(二)事件處理程式JavaScript事件
- mysql 問題處理二則MySql
- sql server 事務處理(二)SQLServer
- JVM如何處理方法呼叫JVM
- php事務處理方法PHP
- 處理問題的方法
- 異常處理方法 (轉)
- 第五篇:資料預處理(二) - 異常值處理
- Python-OpenCV 處理影象(八):影象二值化處理PythonOpenCV
- ACM競賽中用到的二進位制處理方法(完善中)ACM
- android原始碼解析--AlertDialog及AlertDialog.BuilderAndroid原始碼UI
- Linux學習之檔案處理命令(二)目錄處理命令 && 檔案處理命令Linux
- 異常處理機制(二)之異常處理與捕獲
- Flink的流處理API(二)API
- 錯誤碼全域性處理(二)
- Java 傳統異常處理(二)Java
- Java異常處理設計(二)Java
- Db2 日誌處理二DB2
- Toast,popupWindow,AlertDialogAST
- Android影像灰度化、線性灰度變化、二值化處理方法Android
- 處理恢復資料方法
- JavaScript常用的字串處理方法JavaScript字串
- 高併發的處理方法
- ORACLE鎖等待的處理方法Oracle