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
- Python錯誤處理和異常處理(二)Python
- Python資料處理(二):處理 Excel 資料PythonExcel
- 非同步處理方法非同步
- 影像預處理方法
- APM RUEI processor處理程式hang死處理方法
- 專案常用JS方法封裝(二) [ 時間相關處理 ]JS封裝
- JVM如何處理方法呼叫JVM
- Flink的流處理API(二)API
- 二、淺談 JSON 處理技巧JSON
- Android影像灰度化、線性灰度變化、二值化處理方法Android
- 異常處理機制(二)之異常處理與捕獲
- JavaScript常用的字串處理方法JavaScript字串
- 處理恢復資料方法
- Android AlertDialog筆記Android筆記
- 錯誤碼全域性處理(二)
- Netty(二):如何處理io請求?Netty
- 大量time-wait的處理方法AI
- Python 中 Panda 庫 處理表格方法Python
- 資料預處理方法彙總
- create index ORA-00376 處理方法Index
- nodejs程式異常退出處理方法NodeJS
- 自然語言處理:分詞方法自然語言處理分詞
- 處理字串的方法都在這裡字串
- Git處理二進位制檔案Git
- 二、Git 問題彙總及處理Git
- 第二章 處理器結構
- Oracle DG同步失敗故障處理(二)Oracle
- Python 柵格資料處理教程(二)Python
- 日常運維之TX鎖處理(二)運維
- Python使用struct處理二進位制PythonStruct
- Salesforce 大資料量處理篇(二)IndexSalesforce大資料Index
- AlertDialog的初階操作
- SQL Server2008程式堵塞處理方法SQLServer
- php錯誤與異常處理方法PHP
- 影像資料不足時的處理方法
- 爬蟲 | 處理cookie的基本方法——session爬蟲CookieSession
- sklearn中常用資料預處理方法
- node gm `GLIBC_2.14‘ not found 處理方法