Android第三十三期 - Dialog的应用
16lz
2021-01-24
今天遇到一个大难题哦,不过有大牛一眼就瞄出来了,然后就解决了,AlertDialog和Dialog自定义后圆角的处理,如果你跟我一样没有看到这些细节的话就栽了,用AlertDialog不能使得圆角背景透明化,所以要用Dialog处理才行,也就是下面的方法。
Dialog:
private static Dialog mDialog;// 加载gridview中的item的xml方法public static View getView(Context context, int layoutId) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(layoutId, null);return layout;}/** * 显示自定义对话框 * * @param context * @param message * @param listener */public static void showDialog(final Context context, String message,final IAlertDialogButtonListener listener) {View dialogView = null;// Dialog.Builder builder = new Builder(context,// R.style.Theme_Transparent);mDialog = new Dialog(context, R.style.Theme_Transparent);dialogView = getView(context, R.layout.dialog_view);Button btn_ok = (Button) dialogView.findViewById(R.id.btn_ok);Button btn_cancel = (Button) dialogView.findViewById(R.id.btn_cancel);TextView txt_dailog_message = (TextView) dialogView.findViewById(R.id.txt_dailog_message);txt_dailog_message.setText(message);btn_ok.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// 关闭dialogif (mDialog != null) {mDialog.cancel();}// 事件回调if (listener != null) {listener.onClick();}// 播放音效// MyPlayer.playTone(context, MyPlayer.INDEX_STONE_ENTER);}});btn_cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// 关闭dialogif (mDialog != null) {mDialog.cancel();}// 播放音效// MyPlayer.playTone(context, MyPlayer.INDEX_STONE_CANCEL);}});// 为dialog设置View// builder.setView(dialogView);mDialog.setContentView(dialogView);// mDialog = builder.create();mDialog.show();}}
AlertDialog:
/** * 显示自定义对话框 * * @param context * @param message * @param listener */public static void showDialog(final Context context, String message,final IAlertDialogButtonListener listener) {View dialogView = null;AlertDialog.Builder builder = new Builder(context,R.style.Theme_Transparent);dialogView = getView(context, R.layout.dialog_view);ImageButton btn_ok = (ImageButton) dialogView.findViewById(R.id.btn_ok);ImageButton btn_cancel = (ImageButton) dialogView.findViewById(R.id.btn_cancel);TextView txt_dailog_message = (TextView) dialogView.findViewById(R.id.txt_dailog_message);txt_dailog_message.setText(message);btn_ok.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// 关闭dialogif (mAlertDialog != null) {mAlertDialog.cancel();}// 事件回调if (listener != null) {listener.onClick();}// 播放音效MyPlayer.playTone(context, MyPlayer.INDEX_STONE_ENTER);}});btn_cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// 关闭dialogif (mAlertDialog != null) {mAlertDialog.cancel();}// 播放音效MyPlayer.playTone(context, MyPlayer.INDEX_STONE_CANCEL);}});// 为dialog设置Viewbuilder.setView(dialogView);mAlertDialog = builder.create();mAlertDialog.show();}
af_dialog_background圆角:
<?xml version="1.0" encoding="utf-8"?>
dialog_view:
<?xml version="1.0" encoding="utf-8"?>
最后是style:
谢谢hongyang大神~~开心挣钱每一天
更多相关文章
- 采用回调Activity的形式创建AlertDialog单选列表框
- Android滑动菜单框架完全解析,教你如何一分钟实现滑动菜单特效
- Android对话框Dialog,PopupWindow,Toast的实现机制
- Android的Touch事件处理和手势
- Android实现TextView中部分文字监听事件
- android发送模拟按键消息,出现死锁,timeout的解决方法
- Android(安卓)事件分发源码分析
- [置顶] Android触摸事件分发
- 针对 CoordinatorLayout 及 Behavior 的一次细节较真