常用的对话框可以直接调用系统提供的AlertDialog,这过系统提交的对话框比较丑,我们得改写一下:
效果如下:
含确认和取消两按钮的:
只含取消按钮的:
(1)对话框工具类MyAlertDialogUtil.java:
package com.vvpinche.util; import android.app.AlertDialog; import android.content.Context; import android.view.Display; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import com.comotech.vv.R; /** * 自定义对话框工具类 * * @author chenwenbiao * @date 2014-5-14 上午10:58:28 * @version V1.0 */ public class MyAlertDialogUtil { private static String TAG = MyAlertDialogUtil.class.getName(); private Context context = null; private String title = null; private String content = null; private OnClickListener okListener = null; private AlertDialog alertDialog = null; private boolean needCancelButton = true;//是否需要取消按钮 public MyAlertDialogUtil(Context context, String title, String content, boolean needCancelButton , OnClickListener okListener){ this.context = context; this.title = title; this.content = content; this.okListener = okListener; this.needCancelButton = needCancelButton; } /** * 获取对话框示例 * @param context * @param title 提示框的标题 * @param content 提示的内容 * @param needCancelButton 是否需要取消按钮 * @param okListener 只显示确定按钮时,可以不用传okListener参数 * @return */ public static MyAlertDialogUtil getInstance(Context context, String title, String content, boolean needCancelButton , OnClickListener okListener){ return new MyAlertDialogUtil(context , title , content , needCancelButton , okListener); } /** * 使用对话框 */ public void showDialog() { // WindowManager manager = context.getWindowManager(); WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); int width = display.getWidth(); alertDialog = new AlertDialog.Builder(context).create(); alertDialog.show(); Window window = alertDialog.getWindow(); //CLog.d(TAG , "my_alert_dialog_height==========>" + context.getResources().getDimension(R.dimen.my_alert_dialog_height)); //window.setLayout((int) (width * 0.8), (int)(context.getResources().getDimension(R.dimen.my_alert_dialog_height))); window.setLayout((int) (width * 0.8), LayoutParams.WRAP_CONTENT);//高度使用包裹方式,不然在小米1机器上显示不了按钮 // *** 主要就是在这里实现这种效果的. // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容 window.setContentView(R.layout.my_alert_dialog); TextView titleTextView = (TextView) window.findViewById(R.id.my_alert_dialog_title); titleTextView.setText(title); TextView contentTextView = (TextView) window.findViewById(R.id.my_alert_dialog_content); contentTextView.setText(content); View okView = window.findViewById(R.id.my_alert_dialog_ok_layout); okView.setOnClickListener(okListener); if(needCancelButton == true){ /** * 取消按钮 */ View cancelView = window.findViewById(R.id.my_alert_dialog_cancel_layout); cancelView.setVisibility(View.VISIBLE); cancelView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); /** * ok 与 cancel的分隔线 */ View divider = window.findViewById(R.id.my_alert_dialog_cancel_ok_divider); divider.setVisibility(View.VISIBLE); } /** * 只显示确定按钮时,可以不用传okListener参数 */ if(okListener == null){ okView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); } } /** * 取消对话框 */ public void dismiss(){ if(alertDialog != null){ alertDialog.dismiss(); alertDialog = null; } } }
(2)布局文件my_alert_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#222222" android:orientation="vertical" > <TextView android:id="@+id/my_alert_dialog_title" android:layout_width="wrap_content" android:layout_height="65dp" android:layout_gravity="center_vertical" android:gravity="center_vertical" android:paddingLeft="15dp" android:text="快速综合练习" android:textColor="@color/titleTextColor" android:textSize="24sp" /> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="@color/titleSeparatorColor" /> <TextView android:id="@+id/my_alert_dialog_content" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_gravity="center_vertical" android:gravity="center_vertical" android:paddingLeft="15dp" android:text="确认退出吗?" android:textColor="@color/messageTextColor" android:textSize="15sp" /> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="@color/buttonSeparatorColor" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/selector_my_alert_dialog_button" android:orientation="horizontal" > <RelativeLayout android:id="@+id/my_alert_dialog_cancel_layout" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@drawable/selector_my_alert_dialog_button" android:visibility="gone" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="取消" android:textColor="@color/buttonTextColor" /> </RelativeLayout> <View android:id="@+id/my_alert_dialog_cancel_ok_divider" android:layout_width="1dp" android:layout_height="fill_parent" android:background="@color/buttonSeparatorColor" android:visibility="gone" /> <RelativeLayout android:id="@+id/my_alert_dialog_ok_layout" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@drawable/selector_my_alert_dialog_button" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="确定" android:textColor="@color/buttonTextColor" /> </RelativeLayout> </LinearLayout> </LinearLayout>
(3)对话框的颜色配置文件colors.xml
<?xml version="1.0" encoding="UTF-8"?> <resources> <!-- 自定义弹出对话框颜色 --> <color name="titleTextColor">#3eb4d8</color> <color name="titleSeparatorColor">#3eb4d8</color> <color name="messageTextColor">#ffffff</color> <color name="buttonTextColor">#ffffff</color> <color name="buttonSeparatorColor">#12FFFFFF</color> <color name="buttonBackgroundColorNormal">@android:color/transparent</color> <color name="buttonBackgroundColorPressed">#e0000000</color> <color name="buttonBackgroundColorFocused">#e0000000</color> </resources>
(4) 使用简单:
OnClickListener okListener = new OnClickListener() { @Override public void onClick(View v) { hideInputMethod(); finish(); } }; MyAlertDialogUtil myAlertDialogUtil = MyAlertDialogUtil.getInstance(getActivity(),"温馨提示","确认不发生,直接返回?",true,okListener); myAlertDialogUtil.showDialog();
如我的使用如下在点击事件里调用确认对话框:
(1)含确认和取消按钮
/** * 打电话 */ phoneTextView = (TextView)findViewById(R.id.one_route_detail_phone_tv); phoneTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /** * 确认对话框打电话 */ OnClickListener okOnClickListener = new OnClickListener() { @Override public void onClick(View arg0) { Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + route.getPhone())); //启动 startActivity(phoneIntent); myAlertDialogUtil.dismiss();//取消对话框 } }; myAlertDialogUtil = MyAlertDialogUtil.getInstance(getActivity(),"温馨提示","确认打电话吗?",true, okOnClickListener); myAlertDialogUtil.showDialog(); } });
(2)只要确认按钮(needCancelButton=false&&okOnClickListener=null):
/** * 打电话 */ phoneTextView = (TextView)findViewById(R.id.one_route_detail_phone_tv); phoneTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /** * 确认对话框打电话 */ OnClickListener okOnClickListener = new OnClickListener() { @Override public void onClick(View arg0) { Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + route.getPhone())); //启动 startActivity(phoneIntent); myAlertDialogUtil.dismiss();//取消对话框 } }; myAlertDialogUtil = MyAlertDialogUtil.getInstance(getActivity(),"温馨提示","确认打电话吗?",false, null); myAlertDialogUtil.showDialog(); } });
相关推荐
本文实例讲述了Android编程实现在自定义对话框中获取EditText中数据的方法。分享给大家供大家参考,具体如下: 在项目中忽然遇到这样的问题,需要自定义对话框,对话框需要有一个输入框,以便修改所选中的价格,然后...
主要为大家详细介绍了Android中自定义对话框AlertDialog使用示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
绝对给力,你想搞成什么颜色都可以的哦,在xml里面设置,好high的对话框,完全自定义Android对话框AlertDialog的实现,编译通过,代码完整
android 使用alertdialog创建自定义对话框
Adnroid自定义对话框的代码例子。用于演示各类对话框的使用,:简单的AlertDialog、类似Spinner对话框、单选对话框、多选对话框、自定义对话框、滚轮对话框等等。
自定义对话框,效果很多, 使用很漂亮的。 备注写明了多种效果的属性设置 Nifty做的。
可自定义的对话框AlertDialog效果源码,源码mcustom-HerilyAlertDialog-master,完全custom的Android Dialog[HerilyAlertDialog],1.HerilyAlertDialog 是由Herily[虚拟名]研究android对话框源码后的结晶,继承自...
自定义对话框LoadingDialog和Toast,可以添加Gif动画的Dialog,喜欢的自行下载!
自定义AlertDialog对话框用于加载等耗时任务提示,博文地址http://blog.csdn.net/u012806692,在这个地址的内容中找下,因为刚写完还是404。。。。
一个简单的自定义AlertDialog
android漂亮的自定义对话框控件,完全可执行代码,里面附效果图,无论自己使用还是代码参考都比较有意义。
完全自定义Android对话框AlertDialog的实现,在Android2.1下编译通过
自定义对话框demo,解决了自定义View中带有EditView不出现软键盘的问题。
安卓常用自定义动画效果对话框,包括确认,取消,删除等多种效果!
常用的Dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框 AlertDialog的常用方法 setTitle:为对话框设置标题 setMessage:为对话框设置内容 setIcon:为对话框设置图标 ...
前面一篇文章//www.jb51.net/article/103036.htm介绍了alertDialog的四种简单使用,但是有些时候为了让整个app的风格统一,或者说前面的四种形式不符合项目中的需求,这个时候就需要我们自定义alertDialog了。...
实际的项目开发当中,经常需要根据实际的需求来自定义AlertDialog。最近在开发一个WIFI连接的功能,点击WIFI需要弹出自定义密码输入框,具体代码大家参考下本