Android自定义对话框

来源:互联网 发布:sublime 格式化js css 编辑:程序博客网 时间:2024/05/20 09:25

对话框自定义类

package com.yunyou.icloudinn.bookhouse.Ui;/** * Created by chen on 2017/10/13. */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.view.WindowManager;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;import com.yunyou.icloudinn.bookhouse.R;public class CustomDialog extends Dialog {    public CustomDialog(Context context) {        super(context);    }    public CustomDialog(Context context, int theme) {        super(context, theme);    }    public static class Builder {        private Context context;        private String title;        private String message;        private String positiveButtonText;        private String negativeButtonText;        private View contentView;        private CustomDialog dialog;        private View layout;        private DialogInterface.OnClickListener positiveButtonClickListener;        private DialogInterface.OnClickListener negativeButtonClickListener;        public Builder(Context context) {            this.context = context;            LayoutInflater inflater = (LayoutInflater) context                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            // instantiate the dialog with the custom Theme            dialog = new CustomDialog(context,R.style.Dialog);            layout = inflater.inflate(R.layout.dialog_normal_layout, null);            dialog.addContentView(layout, new LayoutParams(                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));            // 全屏            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();            lp.width = WindowManager.LayoutParams.MATCH_PARENT;            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;            dialog.getWindow().setAttributes(lp);        }        public Builder setMessage(String message) {            this.message = message;            return this;        }        /**         * Set the Dialog message from resource         *         * @param         * @return         */        public Builder setMessage(int message) {            this.message = (String) context.getText(message);            return this;        }        /**         * Set the Dialog title from resource         *         * @param title         * @return         */        public Builder setTitle(int title) {            this.title = (String) context.getText(title);            return this;        }        /**         * Set the Dialog title from String         *         * @param title         * @return         */        public Builder setTitle(String title) {            this.title = title;            return this;        }        public Builder setContentView(View v) {            this.contentView = v;            return this;        }        /**         * Set the positive button resource and it's listener         *         * @param positiveButtonText         * @return         */        public Builder setPositiveButton(int positiveButtonText,                                         DialogInterface.OnClickListener listener) {            this.positiveButtonText = (String) context                    .getText(positiveButtonText);            this.positiveButtonClickListener = listener;            return this;        }        public Builder setPositiveButton(String positiveButtonText,                                         DialogInterface.OnClickListener listener) {            this.positiveButtonText = positiveButtonText;            this.positiveButtonClickListener = listener;            return this;        }        public Builder setNegativeButton(int negativeButtonText,                                         DialogInterface.OnClickListener listener) {            this.negativeButtonText = (String) context                    .getText(negativeButtonText);            this.negativeButtonClickListener = listener;            return this;        }        public Builder setNegativeButton(String negativeButtonText,                                         DialogInterface.OnClickListener listener) {            this.negativeButtonText = negativeButtonText;            this.negativeButtonClickListener = listener;            return this;        }        public CustomDialog create() {            // set the dialog title            ((TextView) layout.findViewById(R.id.title)).setText(title);            // set the confirm button            if (positiveButtonText != null) {                ((Button) layout.findViewById(R.id.positiveButton))                        .setText(positiveButtonText);                if (positiveButtonClickListener != null) {                    ((Button) layout.findViewById(R.id.positiveButton))                            .setOnClickListener(new View.OnClickListener() {                                public void onClick(View v) {                                    positiveButtonClickListener.onClick(dialog,                                            DialogInterface.BUTTON_POSITIVE);                                }                            });                }            } else {                // if no confirm button just set the visibility to GONE                layout.findViewById(R.id.positiveButton).setVisibility(                        View.GONE);            }            // set the cancel button//            if (negativeButtonText != null) {//                ((Button) layout.findViewById(R.id.negativeButton))//                        .setText(negativeButtonText);//                if (negativeButtonClickListener != null) {//                    ((Button) layout.findViewById(R.id.negativeButton))//                            .setOnClickListener(new View.OnClickListener() {//                                public void onClick(View v) {//                                    negativeButtonClickListener.onClick(dialog,//                                            DialogInterface.BUTTON_NEGATIVE);//                                }//                            });//                }//            } else {//                // if no confirm button just set the visibility to GONE//                layout.findViewById(R.id.negativeButton).setVisibility(//                        View.GONE);//            }            // set the content message//            if (message != null) {//                ((TextView) layout.findViewById(R.id.message)).setText(message);//            } else if (contentView != null) {//                // if no message set//                // add the contentView to the dialog body//                ((LinearLayout) layout.findViewById(R.id.content))//                        .removeAllViews();//                ((LinearLayout) layout.findViewById(R.id.content))//                        .addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));//            }            dialog.setContentView(layout);            return dialog;        }    }}


对话框自定义布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:clickable="true"    android:background="@color/grey3">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="center"        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: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="120.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>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="60.0dip"        android:layout_centerHorizontal="true"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >        <Button            android:id="@+id/positiveButton"            android:layout_width="114.0dip"            android:layout_height="40.0dip"            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:gravity="center"            android:text="下一张" />    </LinearLayout></RelativeLayout>

对话框样式

     <style name="Dialog" parent="android:style/Theme.Dialog">        <item name="android:background">#00000000</item>        <item name="android:windowFullscreen">true</item>        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:windowNoTitle">true</item>        <item name="android:windowIsFloating">true</item>    </style>

对话框调用

  if(builder==null)builder = new CustomDialog.Builder(context);                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();


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为手机导航信号弱怎么办 华为手机gps信号弱怎么办 麦芒5指纹消失了怎么办 华为应用锁密码忘了怎么办 华为麦芒5密码忘了怎么办 华为卡1无服务怎么办 经常卡1无服务怎么办 华为手机进水无服务怎么办 苹果手机进水后无服务怎么办 苹果6进水无服务怎么办 华为手机突然无服务怎么办 sim卡显示无服务怎么办 华为麦芒进水无限开关机怎么办 华为麦芒5进水黑屏怎么办 华为麦芒6进水了怎么办 4g手机开不开机怎么办 全屏钢化膜总是翘边怎么办 华为麦芒屏幕触屏失灵怎么办 华为麦芒5运行慢怎么办 手机屏保密码忘记了怎么办 麦芒5密码锁忘了怎么办 超薄手机壳松了怎么办 华为麦芒5声音小怎么办 笔记本外壳a面裂了怎么办 苹果手机外壳摔坏了怎么办 挂衣服肩膀出包怎么办 摩拜单车手机号注销了怎么办 摩拜单车手机号码换了怎么办 摩拜单车换手机号码打不开怎么办 摩拜单车丢了怎么办 摩拜单车忘锁了怎么办 透明手机壳粘指纹怎么办 tpu手机壳变黄了怎么办 0pp0手机声音小怎么办 橡胶皮套晒坏了怎么办 宝宝晚上吹空调发烧怎么办 玩手机手指尖疼怎么办 手机型号不支持微信运动怎么办 手腕向下压会疼怎么办 手腕韧带拉伤怎么办恢复快 华为手机用车载充电慢怎么办