【原创】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;
		}
	}
	
}

代码下载: 点击地址

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在安卓中要改变AlertDialog的外观是非常不容易的事情,即便是HoneyComb之后增加了android:singleChoiceItemLayout属性。AlertDialogPro可以让事情变得简单,它包含了AlertDialog的所有功能,同时还具有灵活的自定义功能,代码中还自带了已经定义好的holo和material 两种风格对话框。项目地址:https://github.com/fengdai/AlertDialogPro 效果图:如何使用1.创建AlertDialogPro除了将AlertDialog.Builder替换成AlertDialogPro.Builder之外其他的和AlertDialog没有区别。AlertDialogPro.Builder builder = new AlertDialogPro.Builder(getContext()); builder.setTitle("Title").        setIcon(R.drawable.ic_launcher).        setMessage("Message").        setNeutralButton("Neutral", null).        setPositiveButton("OK", null).        setNegativeButton("Cancel", null).        show();2.holo风格的dialog包含两种holo:Theme.AlertDialogPro.Holo和Theme.AlertDialogPro.Holo.Light只需在style文件中加入alertDialogProTheme属性:<style name="AppTheme" parent="AppBaseTheme">   ...   <!-- Use Holo dark theme as global theme of this app -->   <item name="alertDialogProTheme">@style/Theme.AlertDialogPro.Holo</item> </style>或者是在代码中创建dialog的时候设置:AlertDialogPro.Builder builder = new AlertDialogPro.Builder(getContext(), R.style.Theme_AlertDialogPro_Holo_Light);3.Material风格的dialog包含Theme.AlertDialogPro.Material 和Theme.AlertDialogPro.Material.Light 两种风格使用方法和上面的holo差不多,但是需要AppCompat-v21的支持。4.自定义如果其中某一种风格符合你的绝大部分需求,但是需要做些改动,你可以设置以下属性:<!-- Minimum height of title panel--> <attr name="adpTitleMinHeight" format="dimension" /> <!-- The text appearance for the dialog's message text --> <attr name="adpMessageTextAppearance" format="reference" /> <!-- Minimum height of ListView's items --> <attr name="adpListItemMinHeight" format="dimension" /> <!-- The text color for ListView's items --> <attr name="adpListItemTextColor" format="reference|color" /> <!-- The text appearance for normal ListView's items --> <attr name="adpListItemTextAppearance" format="reference" /> <!-- The text appearance for "multi-choice" ListView's items --> <attr name="adpListMultiChoiceTextAppearance" format="reference" /> <!-- The text appearance for "single-choice" ListView's items --> <attr name="adpListSingleChoiceTextAppearance" format="reference" /> <!-- Divider for the ListView --> <attr name="adpListDivider" format="reference" /> <!-- Selector in a ListView --> <attr name="adpListItemBackground" format="reference" /> <!-- Style for button bars --> <attr name="adpButtonBarStyle" format="reference" /> <!-- Style for buttons within button bars --> <attr name="adpButtonBarButtonStyle" format="reference" /> <!-- Style for the "positive" buttons within button bars --> <attr name="adpButtonBarPositiveButtonStyle" format="reference" /> <!-- Style for the "negative" buttons within button bars --> <attr name="adpButtonBarNegativeButtonStyle" format="reference" /> <!-- Style for the "neutral" buttons within button bars --> <attr name="adpButtonBarNeutralButtonStyle" format="reference" />你甚至可以设置alertdialog的整个布局,如果你需要的是高度自定义的dialog,这是非常重要的。<style name="AlertDialogPro.Material">   <!-- As HoneyComb's android:layout.        Specify your AlertDialogPro's layout -->   <item name="adpLayout">@layout/adp_alert_dialog_material</item>   <!-- As HoneyComb's android:listLayout.        Specify your AlertDialogPro's ListView layout. -->   <item name="adpListLayout">@layout/adp_select_dialog_material</item>   <!-- As HoneyComb's android:listItemLayout.        Specify your AlertDialogPro's list item layout. -->   <item name="adpListItemLayout">@layout/adp_select_dialog_item_material</item>   <!-- As HoneyComb's android:multiChoiceItemLayout.        Specify your AlertDialogPro's multi choice list item layout. -->   <item name="adpMultiChoiceItemLayout">@layout/adp_select_dialog_multichoice_material</item>   <!-- As HoneyComb's android:singleChoiceItemLayout.        Specify your AlertDialogPro's single choice list item layout. -->   <item name="adpSingleChoiceItemLayout">@layout/adp_select_dialog_singlechoice_material</item> </style>使用上面自定义的alertdialog<item name="alertDialogProStyle">@style/AlertDialogPro.Material</item>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值