【原创】Android之修改AlertDialog对话框及使用系统Holo风格
来源:互联网 发布:长相土气的女明星知乎 编辑:程序博客网 时间:2024/05/16 10:48
前一阵子在做伪装密码的功能,需要使用系统的对话框,对话框需要加长按事件等等。哈,直接上代码,我是比较喜欢直接看代码的。
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 {@Overrideprotected 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() {@Overridepublic 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_darkprivate 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_darkprivate 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_darkprivate 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() {@Overridepublic void onClick(View v) {toast("Test click!");}});btn.setOnLongClickListener(new OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {toast("Test long click!");return false;}});}private void toast(String message) {Toast.makeText(this, message, Toast.LENGTH_SHORT).show();}@Overridepublic 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;}}}
代码下载:点击地址
0 0
- 【原创】Android之修改AlertDialog对话框及使用系统Holo风格
- android修改HOLO对话框风格
- android修改HOLO对话框风格
- 修改系统alertdialog对话框风格样式
- android学习之使用AlertDialog创建对话框
- Android之AlertDialog(弹出式对话框)的使用
- Android对话框之AlertDialog
- android之AlertDialog对话框
- Android之AlertDialog对话框
- android之4.0的系统主题style修改android:Theme.Holo.Light
- android之4.0的系统主题style修改android:Theme.Holo.Light
- android 系统提示对话框(AlertDialog)的使用
- android 系统提示对话框(AlertDialog)的使用
- android 系统提示对话框(AlertDialog)的使用
- android 系统提示对话框(AlertDialog)的使用
- android 系统提示对话框(AlertDialog)的使用
- Android 系统提示对话框(AlertDialog)的使用
- Android之警告对话框alertdialog
- 索引浅谈
- 精心收集的Hadoop学习资料
- freemarker静态化例子
- hdu 1503 Advanced Fruits (公共子序列 的输出)
- hdu 4509 湫湫系列故事――减肥记II
- 【原创】Android之修改AlertDialog对话框及使用系统Holo风格
- JAVA 设计模式(Design Patterns)
- java 调用 clojure
- 关于hashmap
- 玩转Web之Jsp(一)-----jsp中的静态包含(<%@include file="url"%>)与动态包含(<jsp:include>)
- ORACLE存储过程运行报“ora-00942表或视图不存在”、“ORA-01031:权限不足”的解决方式
- 百万用户级游戏服务器架构设计(1)
- Servlet 3特性:异步Servlet
- 百万用户级游戏服务器架构设计(2)