【原创】Android之修改AlertDialog对话框及使用系统Holo风格
生活随笔
收集整理的這篇文章主要介紹了
【原创】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 {@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;}}}
代碼下載: 點擊地址
總結
以上是生活随笔為你收集整理的【原创】Android之修改AlertDialog对话框及使用系统Holo风格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS布局解决方案(终结版)
- 下一篇: java垃圾回收文档整理