`
切切歆语
  • 浏览: 29443 次
  • 性别: Icon_minigender_1
  • 来自: 泸州市
社区版块
存档分类
最新评论

自定义Dialog之信息提示

阅读更多

对话框对于应用也是必不可少的一个组件,在Android中也不例外,对话框对于一些提示重要信息,或者一些需要用户额外交互的一些内容很有帮助。

自定义Dialog步骤: 
1、主要创建Java类,并继承Dialog 
2、创建布局文件来加载和一些样式文件

效果图: 

                   

            

DialogBox.java

Java代码
  1. public class DialogBox extends Dialog {  
  2.   
  3.     public DialogBox(Context context) {  
  4.         super(context);  
  5.     }  
  6.   
  7.     public DialogBox(Context context, int theme) {  
  8.         super(context, theme);  
  9.     }  
  10.   
  11.     public static class Builder {  
  12.         private Context context;  
  13.         private String title;  
  14.         private String message;  
  15.         private String positiveButtonText;  
  16.         private String negativeButtonText;  
  17.         private View contentView;  
  18.         private DialogInterface.OnClickListener positiveButtonClickListener;  
  19.         private DialogInterface.OnClickListener negativeButtonClickListener;  
  20.   
  21.         public Builder(Context context) {  
  22.             this.context = context;  
  23.         }  
  24.   
  25.         public Builder setMessage(String message) {  
  26.             this.message = message;  
  27.             return this;  
  28.         }  
  29.   
  30.         /** 
  31.          * Set the Dialog message from resource 
  32.          *  
  33.          * @param title 
  34.          * @return 
  35.          */  
  36.         public Builder setMessage(int message) {  
  37.             this.message = (String) context.getText(message);  
  38.             return this;  
  39.         }  
  40.   
  41.         /** 
  42.          * Set the Dialog title from resource 
  43.          *  
  44.          * @param title 
  45.          * @return 
  46.          */  
  47.         public Builder setTitle(int title) {  
  48.             this.title = (String) context.getText(title);  
  49.             return this;  
  50.         }  
  51.   
  52.         /** 
  53.          * Set the Dialog title from String 
  54.          *  
  55.          * @param title 
  56.          * @return 
  57.          */  
  58.   
  59.         public Builder setTitle(String title) {  
  60.             this.title = title;  
  61.             return this;  
  62.         }  
  63.   
  64.         public Builder setContentView(View v) {  
  65.             this.contentView = v;  
  66.             return this;  
  67.         }  
  68.   
  69.         /** 
  70.          * Set the positive button resource and it's listener 
  71.          *  
  72.          * @param positiveButtonText 
  73.          * @return 
  74.          */  
  75.         public Builder setPositiveButton(int positiveButtonText,  
  76.                 DialogInterface.OnClickListener listener) {  
  77.             this.positiveButtonText = (String) context  
  78.                     .getText(positiveButtonText);  
  79.             this.positiveButtonClickListener = listener;  
  80.             return this;  
  81.         }  
  82.   
  83.         public Builder setPositiveButton(String positiveButtonText,  
  84.                 DialogInterface.OnClickListener listener) {  
  85.             this.positiveButtonText = positiveButtonText;  
  86.             this.positiveButtonClickListener = listener;  
  87.             return this;  
  88.         }  
  89.   
  90.         public Builder setNegativeButton(int negativeButtonText,  
  91.                 DialogInterface.OnClickListener listener) {  
  92.             this.negativeButtonText = (String) context  
  93.                     .getText(negativeButtonText);  
  94.             this.negativeButtonClickListener = listener;  
  95.             return this;  
  96.         }  
  97.   
  98.         public Builder setNegativeButton(String negativeButtonText,  
  99.                 DialogInterface.OnClickListener listener) {  
  100.             this.negativeButtonText = negativeButtonText;  
  101.             this.negativeButtonClickListener = listener;  
  102.             return this;  
  103.         }  
  104.   
  105.         public DialogBox create() {  
  106.             LayoutInflater inflater = (LayoutInflater) context  
  107.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  108.             final DialogBox dialog = new DialogBox(context, R.style.Dialog);  
  109.             View layout = inflater.inflate(R.layout.dialog_normal_layout, null);  
  110.             dialog.addContentView(layout, new LayoutParams(  
  111.                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  112.             ((TextView) layout.findViewById(R.id.title)).setText(title);  
  113.             if (positiveButtonText != null) {  
  114.                 ((Button) layout.findViewById(R.id.positiveButton))  
  115.                         .setText(positiveButtonText);  
  116.                 if (positiveButtonClickListener != null) {  
  117.                     ((Button) layout.findViewById(R.id.positiveButton))  
  118.                             .setOnClickListener(new View.OnClickListener() {  
  119.                                 public void onClick(View v) {  
  120.                                     positiveButtonClickListener.onClick(dialog,  
  121.                                             DialogInterface.BUTTON_POSITIVE);  
  122.                                 }  
  123.                             });  
  124.                 }  
  125.             } else {  
  126.                 layout.findViewById(R.id.positiveButton).setVisibility(  
  127.                         View.GONE);  
  128.             }  
  129.             if (negativeButtonText != null) {  
  130.                 ((Button) layout.findViewById(R.id.negativeButton))  
  131.                         .setText(negativeButtonText);  
  132.                 if (negativeButtonClickListener != null) {  
  133.                     ((Button) layout.findViewById(R.id.negativeButton))  
  134.                             .setOnClickListener(new View.OnClickListener() {  
  135.                                 public void onClick(View v) {  
  136.                                     negativeButtonClickListener.onClick(dialog,  
  137.                                             DialogInterface.BUTTON_NEGATIVE);  
  138.                                 }  
  139.                             });  
  140.                 }  
  141.             } else {  
  142.                 layout.findViewById(R.id.negativeButton).setVisibility(  
  143.                         View.GONE);  
  144.             }  
  145.             if (message != null) {  
  146.                 ((TextView) layout.findViewById(R.id.message)).setText(message);  
  147.             } else if (contentView != null) {  
  148.                 ((LinearLayout) layout.findViewById(R.id.content))  
  149.                         .removeAllViews();  
  150.                 ((LinearLayout) layout.findViewById(R.id.content)).addView(  
  151.                         contentView, new LayoutParams(LayoutParams.FILL_PARENT,  
  152.                                 LayoutParams.FILL_PARENT));  
  153.             }  
  154.             dialog.setContentView(layout);  
  155.             return dialog;  
  156.         }  
  157.   
  158.     }  
  159. }  



自定义View的布局文件:

 


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:orientation="vertical"
    android:padding="20.0dip" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FA90BC"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            style="@style/text_18_ffffff"
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:gravity="center"
            android:text="提示"
            android:visibility="visible" />

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:gravity="center" >

            <TextView
                android:id="@+id/message"
                style="@style/text_16_666666"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="left|center"
                android:lineSpacingMultiplier="1.5"
                android:minHeight="65.0dip"
                android:paddingBottom="15.0dip"
                android:paddingLeft="20.0dip"
                android:paddingRight="20.0dip"
                android:paddingTop="15.0dip" />
        </LinearLayout>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1.0px"
            android:background="#ffd0d0d0" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="60.0dip"
            android:layout_gravity="bottom"
            android:background="@drawable/dialog_bottom_bg"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/positiveButton"
                style="@style/text_15_ffffff_sdw"
                android:layout_width="114.0dip"
                android:layout_height="40.0dip"
                android:background="#FA90BC"
                android:gravity="center"
                android:text="确定" />

            <Button
                android:id="@+id/negativeButton"
                style="@style/text_15_666666_sdw"
                android:layout_width="114.0dip"
                android:layout_height="40.0dip"
                android:layout_marginLeft="20.0dip"
                android:background="#ffffff"
                android:gravity="center"
                android:text="取消" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>




MainActivity.Java

 

 

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}


	public void showAlertDialog(View view) {

		DialogBox.Builder builder = new DialogBox.Builder(this);
		builder.setMessage("删除该商品?");
		builder.setTitle("温馨提示");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				// 你的操作事项
			}
		});

		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});

		builder.create().show();

	}
}

 

源码点击下载

分享到:
评论

相关推荐

    android 自定义Dialog提示+动画效果

    本文将深入探讨如何实现一个带有动画效果的自定义Dialog,并以"android 自定义Dialog提示+动画效果"为主题,结合提供的压缩包文件`XAlertDialogLibrary`,详细讲解相关知识点。 首先,我们来理解什么是Dialog。在...

    QT自定义dialog提示窗

    本项目“QT自定义dialog提示窗”着重于通过纯代码方式创建对话框,无需使用UI文件。下面将详细阐述相关知识点。 1. **自定义对话框的创建**: 在QT中,可以继承`QDialog`类来创建自定义对话框。`QDialog`是QT中的...

    Android自定义Dialog多选对话框(Dialog+Listview+CheckBox)

    在Android开发中,自定义Dialog是一种常见的需求,用于提供一种轻量级的用户交互界面,如提示信息或者进行选择操作。本示例是关于如何创建一个具有多选功能的Dialog,结合了Dialog、ListView和CheckBox的使用。下面...

    ANdroid自定义Dialog及原生Dialog

    在某些情况下,我们可能希望创建一种介于原生Dialog和自定义Dialog之间的提示框,它保持了原生风格,但可以做一些小幅度的定制。这可以通过设置AlertDialog的View来实现: ```java View customView = ...

    自定义dialog实现登录框

    在Android开发中,自定义Dialog是一种常见的用户界面(UI)设计技术,用于提供与用户交互的弹出式窗口。通常,我们使用Dialog来显示重要的信息、提示或进行简单的操作,如登录、注册等。本教程将详细介绍如何在...

    自定义Dialog简单解决多级菜单多选问题

    在Android开发中,自定义Dialog是一种常见的用户交互方式,它能提供更为丰富的界面展示和定制化功能。在处理多级菜单多选问题时,传统的做法可能会导致代码复杂度增加,逻辑难以维护。本文将深入探讨如何通过自定义...

    各种自定义dialog

    在Android开发中,自定义Dialog是一种常见的需求,用于提供用户交互和信息展示的窗口。"各种自定义dialog"这个主题涵盖了几个重要的自定义Dialog类型,包括自定义对话框、多选对话框、单选对话框以及最简单的对话框...

    自定义dialog弹出框

    在Android开发中,自定义Dialog弹出框是一个常见的需求,它可以帮助我们提供更丰富的用户交互体验,使得信息提示或者操作选择更加个性化。本篇将详细讲解如何创建和使用自定义Dialog,以及涉及到的相关知识点。 ...

    Android自定义Dialog弹窗提示

    默认的Dialog样式可能无法满足所有设计需求,因此开发者常常需要自定义Dialog来实现特定的视觉效果和功能。本篇将详细介绍如何在Android中创建自定义的Dialog弹窗提示。 首先,我们需要理解Dialog的基本结构。...

    android 自定义dialog Demo

    在DialogSample项目中,可能还会包含一些关于如何使用工具(如Android Studio的布局预览、调试技巧等)的提示,以帮助开发者更好地理解和实现自定义Dialog。 总的来说,自定义Dialog是提升Android应用用户体验的...

    android自定义dialog和Toast

    在Android开发中,自定义Dialog和Toast是提升用户体验和界面个性化的重要手段。Dialog通常用于向用户展示重要信息或需要用户做出决策的情况,而Toast则用于轻量级的通知,不打断用户的当前操作。以下是对如何自定义...

    各种自定义dialog合集

    自定义Dialog不仅能够满足基本的提示功能,还可以根据应用的风格和需求进行定制,从而提升用户体验。以下是对给定文件中涉及的自定义Dialog知识点的详细说明: 1. **基本的对话框.rar**: 这个文件可能包含了一些...

    自定义Dialog样式+大小+位置

    在Android开发中,Dialog是一种常见的用户交互界面,用于在主线程中显示临时的通知或提示信息。默认的Dialog样式和尺寸往往不能满足所有开发者的需求,因此,了解如何自定义Dialog的样式、大小和位置是非常重要的...

    百度地图中自定义Dialog

    在本教程中,我们将深入探讨如何在百度地图API中创建自定义Dialog,以便为用户提供更加个性化的交互体验。 首先,我们需要了解Dialog的基本概念。Dialog是Android系统提供的一种UI组件,它浮于应用程序的主窗口之上...

    自定义Dialog加载动画

    Dialog是Android系统提供的一种用户界面组件,用于在主界面之上显示一个临时的窗口,通常用来提示用户一些信息或请求用户的输入。默认的Dialog样式可能无法满足所有设计需求,因此自定义Dialog就显得尤为重要。 在...

    dialog自定义

    在Android开发中,Dialog是一种非常常见的用户交互组件,它用于显示一些临时性的信息或提示,通常不会占据整个屏幕。然而,系统默认的Dialog样式有限,为了满足更丰富的设计需求,开发者常常需要对Dialog进行自定义...

    android自定义dialog加载窗

    Dialog是Android系统提供的一种轻量级窗口,它漂浮在应用主窗口之上,可以用来提示用户信息或进行简单的交互操作。默认的Dialog样式可能无法满足所有设计需求,因此开发者经常需要自定义其外观和行为。 2. **...

    android 自定义Dialog下载文件

    android自定义Dialog下载文件 ,在自定义的Dialog上显示文件下载的速度和ProgressBar进度,共享给大家! 欢迎指点提议 Email:vipa1888@163.com QQ 840950105 Author: spring sky

Global site tag (gtag.js) - Google Analytics