日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

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

發(fā)布時(shí)間:2023/12/10 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【原创】Android之修改AlertDialog对话框及使用系统Holo风格 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前一陣子在做偽裝密碼的功能,需要使用系統(tǒng)的對(duì)話框,對(duì)話框需要加長(zhǎng)按事件等等。哈,直接上代碼,我是比較喜歡直接看代碼的。

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.?使用系統(tǒng)Holo風(fēng)格

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 {/*** 生成符合系統(tǒng)主題的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");// 這里設(shè)置點(diǎn)擊事件null,再重新寫(xiě)點(diǎn)擊事件,屏蔽點(diǎn)擊之后對(duì)話框消失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;}}}
代碼下載: 點(diǎn)擊地址

總結(jié)

以上是生活随笔為你收集整理的【原创】Android之修改AlertDialog对话框及使用系统Holo风格的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。