package com.example.fragments; import java.lang.ref.WeakReference; import java.lang.reflect.Field; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; /** * <p> * 关于DialogFragment的使用 * </p> * 下午5:38:45 * * @auther delvikCoder */ public class CustomFragment extends DialogFragment { private static Context mContext; private AlertDialog alertDialog; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { String str = getArguments().getString("title"); alertDialog = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT).setTitle(str) .setMessage("这是个测试DialogFragment的列子") .setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setCancelable(false) .create(); setButtonUseless(); return alertDialog; } @Override public void show(FragmentManager manager, String tag) { super.show(manager, tag); } /** * <p> * 这里利用反射使我们的AlertDialog在没特定使其消失情况下,点击按钮不消失 具体看:<a * href="http://blog.csdn.net/abby_dcy/article/details/6049535">看博客</a> * </p> * void */ public void setButtonUseless() { Field field; try { field = alertDialog.getClass().getDeclaredField("mAlert"); field.setAccessible(true); Object obj = field.get(alertDialog); field = obj.getClass().getDeclaredField("mHandler"); field.setAccessible(true); field.set(obj, new ButtonHandler(null)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private static final class ButtonHandler extends Handler { // Button clicks have Message.what as the BUTTON{1,2,3} constant private static final int MSG_DISMISS_DIALOG = 1; private WeakReference<DialogInterface> mDialog; public ButtonHandler(DialogInterface dialog) { mDialog = new WeakReference<DialogInterface>(dialog); } @Override 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); break; // 下面这两句导致dialog消失 // case MSG_DISMISS_DIALOG: // ((DialogInterface) msg.obj).dismiss(); } } } /** * <p> * 标题 * </p> * * @param title * @return Fragment */ public static CustomFragment getFragment(Context context, String title) { mContext = context; CustomFragment fragment = new CustomFragment(); Bundle b = new Bundle(); b.putString("title", title); fragment.setArguments(b); return fragment; } }
Button btn = (Button) findViewById(R.id.button1ss); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { CustomFragment customFragment = (CustomFragment) CustomFragment .getFragment(MainActivity.this, "AlertDialog.THEME_HOLO_LIGHT"); customFragment.show(getSupportFragmentManager(), ""); } });
THEME_DEVICE_DEFAULT_LIGHT,THEME_TRADITIONAL,THEME_DEVICE_DEFAULT_DARK,THEME_HOLO_LIGHT
默认AlertDialog不管点击那个按钮都会消失,设置AlertDialog不消失,而有时候我们不想 其消失:
/** * <p> * 这里利用反射使我们的AlertDialog在没特定使其消失情况下,点击按钮不消失 具体看:<a * href="http://blog.csdn.net/abby_dcy/article/details/6049535">看博客</a> * </p> * void */ public void setButtonUseless() { Field field; try { field = alertDialog.getClass().getDeclaredField("mAlert"); field.setAccessible(true); Object obj = field.get(alertDialog); field = obj.getClass().getDeclaredField("mHandler"); field.setAccessible(true); field.set(obj, new ButtonHandler(null)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
static int resolveDialogTheme(Context context, int resid) { if (resid == THEME_TRADITIONAL) { return com.android.internal.R.style.Theme_Dialog_Alert; } else if (resid == THEME_HOLO_DARK) { return com.android.internal.R.style.Theme_Holo_Dialog_Alert; } else if (resid == THEME_HOLO_LIGHT) { return com.android.internal.R.style.Theme_Holo_Light_Dialog_Alert; } else if (resid == THEME_DEVICE_DEFAULT_DARK) { return com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert; } else if (resid == THEME_DEVICE_DEFAULT_LIGHT) { return com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog_Alert; } else if (resid >= 0x01000000) { // start of real resource IDs. return resid; } else { TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme, outValue, true); return outValue.resourceId; } }
private AlertController mAlert;