【原创】Android之修改AlertDialog对话框及使用系统Holo风格

前一阵子在做伪装密码的功能,需要使用系统的对话框,对话框需要加长按事件等等。哈,直接上代码,我是比较喜欢直接看代码的。

1. 获取AlertDialog的Title

final int alertTitleId = getResources().getIdentifier( "alertTitle", "id", "android" );
TextView title = (TextView)dlg.findViewById(alertTitleId);

2. 获取AlertDialog的Message

TextView message = (TextView)dlg.findViewById(android.R.id.message);

3. 获取AlertDialog的Button

Button btn = (Button)dlg.getButton(DialogInterface.BUTTON_POSITIVE);

4. 使用系统Holo风格

AlertDialog.Builder builder = DialogUtils.getAlertDialog(this, isLightDialog);

public static AlertDialog.Builder getAlertDialog(Context context, boolean isLight) {
		return new AlertDialog.Builder(
				new ContextThemeWrapper(context, getDialogTheme(isLight)));
	}
	
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static int getDialogTheme(boolean isLight) {
	return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
			(isLight ? android.R.style.Theme_Holo_Light_Dialog
			: android.R.style.Theme_Holo_Dialog)
			: android.R.style.Theme_Dialog;
}

全部代码:

public class DialogUtils {
	/**
	 * 生成符合系统主题的AlertDialog.Builder
	 * @param context
	 * @return
	 */
	public static AlertDialog.Builder getAlertDialog(Context context, boolean isLight) {
		return new AlertDialog.Builder(
				new ContextThemeWrapper(context, getDialogTheme(isLight)));
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public static int getDialogTheme(boolean isLight) {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
				(isLight ? android.R.style.Theme_Holo_Light_Dialog
				: android.R.style.Theme_Holo_Dialog)
				: android.R.style.Theme_Dialog;
	}
	
}

public class MainActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView();
	}

	private void initView() {
		initButtonView();
	}
	
	private void initButtonView() {
		findViewById(R.id.btn_show_dialog).setOnClickListener(this);
		findViewById(R.id.btn_show_dialog_listener).setOnClickListener(this);
	}
	
	private void showDialogView() {
		AlertDialog dlg = creatAlertDialog(true);
		dlg.show();
		setAlertDialog(dlg);
	}
	
	private void showDialogViewOnShowListener() {
		AlertDialog dlg = creatAlertDialog(false);
		dlg.setOnShowListener(new OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				setAlertDialog((AlertDialog)dialog);
			}
		});
		dlg.show();
	}
	
	private void setAlertDialog(AlertDialog dlg) {
		Typeface typeFace = Typeface.createFromAsset(getAssets(),
				"fonts/RobotoCondensed-Italic.ttf");
		setAlertDialogTitle(dlg, typeFace);
		setAlertDialogMessage(dlg, typeFace);
		setAlertDialogBtnView(dlg, typeFace);
	}
	
	private AlertDialog creatAlertDialog(boolean isLightDialog) {
		AlertDialog.Builder builder = DialogUtils.getAlertDialog(this, isLightDialog);
		builder.setTitle("Test Title");
		builder.setMessage("Test Message");
		// 这里设置点击事件null,再重新写点击事件,屏蔽点击之后对话框消失
		builder.setPositiveButton("Test Button", null);
		AlertDialog dlg = builder.create();
		return dlg;
	}
	
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)// android.R.color.holo_blue_dark
	private void setAlertDialogTitle(AlertDialog dlg, Typeface typeFace) {
		final int alertTitleId = getResources().getIdentifier( "alertTitle", "id", "android" );
		TextView title = (TextView)dlg.findViewById(alertTitleId);
		title.setTextColor(getResources().getColor(android.R.color.holo_blue_bright));
		if (null != typeFace) {
			title.setTypeface(typeFace);
		}
	}
	
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)// android.R.color.holo_blue_dark
	private void setAlertDialogMessage(AlertDialog dlg, Typeface typeFace) {
		TextView message = (TextView)dlg.findViewById(android.R.id.message);
		message.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
		if (null != typeFace) {
			message.setTypeface(typeFace);
		}
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)// android.R.color.holo_blue_dark
	private void setAlertDialogBtnView(AlertDialog dlg, Typeface typeFace) {
		Button btn = (Button)dlg.getButton(DialogInterface.BUTTON_POSITIVE);
		btn.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
		if (null != typeFace) {
			btn.setTypeface(typeFace);
		}
		setButtonClickEvent(btn);
	}
	
	private void setButtonClickEvent(Button btn) {
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				toast("Test click!");
			}
		});
		btn.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				toast("Test long click!");
				return false;
			}
		});
	}
	
	private void toast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_show_dialog:
			showDialogView();
			break;
		case R.id.btn_show_dialog_listener:
			showDialogViewOnShowListener();
			break;
		default:
			break;
		}
	}
	
}

代码下载: 点击地址

你可能感兴趣的:(Android)