Android自定义对话框Dialog

在进行android项目中需要用到对话框,但是由于系统自带的对话框样式不能很好的和你的项目融合,于是就需要用到自定义对话框了。自定义对话框能提高应用的档次。

在进行android项目中需要用到对话框,但是由于系统自带的对话框样式不能很好的和你的项目融合,于是就需要用到自定义对话框了。自定义对话框能提高应用的档次。效果图如下:

Android自定义对话框Dialog

具体怎么实现我不细说了,直接把源码粘贴出来。源码如下:

my_dialog.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="
https://schemas.android.com/apk/res/android
"  
    android:orientation="vertical"   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:minWidth="280dip"  
    android:background="#FFFFFFFF">  
      
    <TextView  
        android:id="@+id/dialog_title"  
        android:layout_width="fill_parent"  
        android:layout_height="40.0dip"  
        android:gravity="center_vertical"  
        android:paddingLeft="10.0dip"  
        android:text="@string/dialog_title"  
        android:textSize="20dip"  
        android:textStyle="bold"  
        android:textColor="#0087CB"/>  
      
    <ImageView  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent"  
        android:background="@drawable/dialog_sparator"/>  
      
    <LinearLayout   
        android:id="@+id/dialog_content"  
        android:orientation="vertical"  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent">  
        <TextView  
            android:id="@+id/dialog_message"  
            android:layout_width="fill_parent"  
            android:layout_height="50.0dip"  
            android:gravity="center_vertical"  
            android:paddingLeft="10dip"  
            android:text="@string/dialog_msg"  
            android:textSize="20dip"  
            android:textStyle="bold"  
            android:textColor="#707070"/>  
    </LinearLayout>  
      
    <LinearLayout  
        android:orientation="horizontal"  
        android:layout_width="fill_parent"  
        android:layout_height="50.0dip"  
        android:layout_alignParentTop="true"  
        android:gravity="center">  
        <Button  
            android:id="@+id/dialog_back"  
            android:layout_width="120dip"  
            android:layout_height="wrap_content"  
            android:layout_marginRight="5dip"  
            android:text="@string/dialog_back"  
            android:textSize="20dip"  
            android:textColor="#ffffff"  
            android:textStyle="bold"  
            android:background="@drawable/dlg_bckbtn_selector"/>  
        <Button  
            android:id="@+id/dialog_confirm"  
            android:layout_width="120dip"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="5dip"  
            android:text="@string/dialog_exit"  
            android:textSize="20dip"  
            android:textColor="#ffffff"  
            android:textStyle="bold"  
            android:background="@drawable/dlg_cfgrmbtn_selector"/>  
    </LinearLayout>  
              
</LinearLayout>

 定义Dialog的样式(主题):

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  
    <!-- 定义对话框样式 -->  
    <style name="Dialog" parent="android:style/Theme.Dialog">  
        <item name="android:windowBackground">@null</item>  
        <item name="android:windowNoTitle">true</item>  
        <item name="android:windowIsFloating">true</item>  
    </style>  
      
</resources>

 自定义Dialog的Java代码:
Java代码

package com.book;  
  
import android.app.Dialog;  
import android.content.Context;  
import android.content.DialogInterface;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup.LayoutParams;  
import android.widget.Button;  
import android.widget.LinearLayout;  
import android.widget.TextView;  
   
  
public class MyDialog extends Dialog {  
   
    public MyDialog(Context context, int theme) {  
        super(context, theme);  
    }  
   
    public MyDialog(Context context) {  
        super(context);  
    }  
   
    /** 
     * Helper class for creating a custom dialog 
     */  
    public static class Builder {  
        private Context context;  
        private String title; // 对话框标题  
        private String message; // 对话框内容  
        private String backButtonText; // 对话框返回按钮文本  
        private String confirmButtonText; // 对话框确定文本  
        private View contentView;  
   
        // 对话框按钮监听事件  
        private DialogInterface.OnClickListener   
                        backButtonClickListener,  
                        confirmButtonClickListener;  
   
        public Builder(Context context) {  
            this.context = context;  
        }  
   
        /** 
         * 使用字符串设置对话框消息 
         * @param title 
         * @return 
         */  
        public Builder setMessage(String message) {  
            this.message = message;  
            return this;  
        }  
   
        /** 
         * 使用资源设置对话框消息 
         * @param title 
         * @return 
         */  
        public Builder setMessage(int message) {  
            this.message = (String) context.getText(message);  
            return this;  
        }  
   
        /** 
         * 使用资源设置对话框标题信息 
         * @param title 
         * @return 
         */  
        public Builder setTitle(int title) {  
            this.title = (String) context.getText(title);  
            return this;  
        }  
   
        /** 
         * 使用字符串设置对话框标题信息 
         * @param title 
         * @return 
         */  
        public Builder setTitle(String title) {  
            this.title = title;  
            return this;  
        }  
   
        /** 
         * 设置自定义的对话框内容 
         * @param v 
         * @return 
         */  
        public Builder setContentView(View v) {  
            this.contentView = v;  
            return this;  
        }  
   
        /** 
         * 设置back按钮的事件和文本 
         * @param backButtonText 
         * @param listener 
         * @return 
         */  
        public Builder setBackButton(int backButtonText, DialogInterface.OnClickListener listener) {  
            this.backButtonText = (String)context.getText(backButtonText);  
            this.backButtonClickListener = listener;  
            return this;  
        }  
   
        /** 
         * 设置back按钮的事件和文本 
         * @param backButtonText 
         * @param listener 
         * @return 
         */  
        public Builder setBackButton(String backButtonText, DialogInterface.OnClickListener listener) {  
            this.backButtonText = backButtonText;  
            this.backButtonClickListener = listener;  
            return this;  
        }  
   
        /** 
         * 设置确定按钮事件和文本 
         * @param confirmButtonText 
         * @param listener 
         * @return 
         */  
        public Builder setConfirmButton(int confirmButtonText, DialogInterface.OnClickListener listener) {  
            this.confirmButtonText = (String)context.getText(confirmButtonText);  
            this.confirmButtonClickListener = listener;  
            return this;  
        }  
   
        /** 
         * 设置确定按钮事件和文本 
         * @param negativeButtonText 
         * @param listener 
         * @return 
         */  
        public Builder setConfirmButton(String confirmButtonText, DialogInterface.OnClickListener listener) {  
             this.confirmButtonText = confirmButtonText;  
             this.confirmButtonClickListener = listener;  
             return this;  
        }  
   
        /** 
         * 创建自定义的对话框 
         */  
        public MyDialog create() {  
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
              
            // 实例化自定义的对话框主题  
            final MyDialog dialog = new MyDialog(context, R.style.Dialog);  
              
            View layout = inflater.inflate(R.layout.my_dialog, null);  
            dialog.addContentView(layout,   
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
              
            // 设置对话框标题  
            ((TextView) layout.findViewById(R.id.dialog_title)).setText(title);  
              
            // 设置对话框内容  
            if (message != null) {  
                TextView dlgMsg = (TextView)layout.findViewById(R.id.dialog_message);  
                dlgMsg.setText(message);  
            } else if (contentView != null) {  
                // if no message set  
                // 如果没有设置对话框内容,添加contentView到对话框主体  
                ((LinearLayout) layout.findViewById(R.id.dialog_content)).removeAllViews();  
                ((LinearLayout) layout.findViewById(R.id.dialog_content)).addView(  
                    contentView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));  
            }  
              
            // 设置返回按钮事件和文本  
            if (backButtonText != null) {  
                Button bckButton = ((Button) layout.findViewById(R.id.dialog_back));  
                bckButton.setText(backButtonText);  
                  
                if (backButtonClickListener != null) {  
                    bckButton.setOnClickListener(new View.OnClickListener() {  
                        public void onClick(View v) {  
                            backButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);  
                        }  
                    });  
                }  
            } else {  
                layout.findViewById(R.id.dialog_back).setVisibility(View.GONE);  
            }  
              
            // 设置确定按钮事件和文本  
            if (confirmButtonText != null) {  
                Button cfmButton = ((Button) layout.findViewById(R.id.dialog_confirm));  
                cfmButton.setText(confirmButtonText);  
                  
                if (confirmButtonClickListener != null) {  
                    cfmButton.setOnClickListener(new View.OnClickListener() {  
                        public void onClick(View v) {  
                            confirmButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);  
                        }  
                    });  
                }  
            } else {  
                layout.findViewById(R.id.dialog_confirm).setVisibility(View.GONE);  
            }       
            dialog.setContentView(layout);      
            return dialog;  
        }  
    }  
}

 使用自定义的Dialog:

@Override  
protected Dialog onCreateDialog(int id) {  
    Dialog dialog = null;  
    switch (id) {  
    case DIALOG_EXIT:  
        MyDialog.Builder myBuilder = new MyDialog.Builder(MainActivity.this);  
        myBuilder.setTitle("温馨提示");  
        myBuilder.setMessage("您确定退出吗?");  
        myBuilder.setBackButton("返 回", new DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface arg0, int arg1) {  
                // 关闭对话框  
                dismissDialog(DIALOG_EXIT);  
            }  
        });  
        myBuilder.setConfirmButton("确 定", new DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface arg0, int arg1) {  
                // 退出电子书  
                finish();  
            }  
        });  
        dialog = myBuilder.create();  
        break;  
    }  
    return dialog;  
}

 当点击返回菜单时显示对话框:

@Override  
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // 监听主菜单返回事件  
    if (keyCode == KeyEvent.KEYCODE_BACK) {  
        showDialog(DIALOG_EXIT);  
        return false;  
    }  
    return super.onKeyDown(keyCode, event);  
}

「资源下载」

成熟不是人的心变老,而是泪在眼眶里打转还能微笑。
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号