手记

自定义Dialog之信息提示

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

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

效果图: 

            

DialogBox.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) context107                     .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"
            
            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"
                
                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"
                
                android:layout_width="114.0dip"
                android:layout_height="40.0dip"
                android:background="#FA90BC"
                android:gravity="center"
                android:text="确定" />

            <Button                android:id="@+id/negativeButton"
                
                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();

    }
}

 

源码下载:http://download.csdn.net/detail/dickyqie/9649758

原文链接:http://www.apkbus.com/blog-894741-63367.html

0人推荐
随时随地看视频
慕课网APP