美文网首页
点击button弹出对话框

点击button弹出对话框

作者: it奔跑在路上 | 来源:发表于2018-11-05 17:34 被阅读0次

    常规的对话框如下:


    常规的对话框.jpg

    cancelButtonText可以设置为null,不显示

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button button = (Button) findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseDialog dialog = new BaseDialog(MainActivity.this,
                            "发现新版本,马上更新?", -1, "取消",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //取消
                                    Log.v("666", "777");
                                }
                            }, "确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //确定
                            Log.v("666", "888");
                        }
                    });
                    //设置强制更新
                    dialog.setCanceledOnTouchOutside(false);
                    dialog.setCancelable(false);
                    //显示对话框
                    dialog.show();
                }
            });
    
        }
    }
    
    /**
     * 基本的对话框样式
     */
    public class BaseDialog extends Dialog implements View.OnClickListener {
        private Context context;
        private View contentView;
        private OnClickListener cancelButtonClickListener, confirmButtonClickListener;
        private boolean isAutoDismiss = true;
        private boolean canceledOnTouchOutside = true;
    
        public BaseDialog(Context context, String title, int contentLayout,
                          String cancelButtonText, OnClickListener cancelButtonClickListener,
                          String confirmButtonText, OnClickListener confirmButtonClickListener) {
            this(context, R.style.BaseDialogStyle, title, contentLayout,
                    cancelButtonText, cancelButtonClickListener,
                    confirmButtonText, confirmButtonClickListener);
        }
    
        public BaseDialog(Context context, int theme, String title, int contentLayoutId,
                          String cancelButtonText, OnClickListener cancelButtonClickListener,
                          String confirmButtonText, OnClickListener confirmButtonClickListener) {
            super(context, theme);
            this.context = context;
            this.cancelButtonClickListener = cancelButtonClickListener;
            this.confirmButtonClickListener = confirmButtonClickListener;
    
            //root view
            View rootView = LayoutInflater.from(context).inflate(R.layout.base_dialog, null);
            TextView titleTextView = (TextView) rootView.findViewById(R.id.base_dialog_title);
            if (titleTextView != null) {
                titleTextView.setText(title);
            }
    
            //title
            View titleLayout = rootView.findViewById(R.id.base_dialog_title_layout);
            if (titleLayout != null && TextUtils.isEmpty(title)) {
                titleLayout.setVisibility(View.GONE);
            }
    
            //body
            ViewGroup bodyLayout = (ViewGroup) rootView.findViewById(R.id.base_dialog_content_layout);
            if (contentLayoutId > 0) {
                bodyLayout.setVisibility(View.VISIBLE);
                View contentLayout = LayoutInflater.from(context).inflate(contentLayoutId, null);
                bodyLayout.addView(contentLayout);
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) contentLayout.getLayoutParams();
                params.width = FrameLayout.LayoutParams.MATCH_PARENT;
                params.height = FrameLayout.LayoutParams.WRAP_CONTENT;
                contentLayout.setLayoutParams(params);
            }else {
                bodyLayout.setVisibility(View.GONE);
            }
    
            //separator
            View separatorView = rootView.findViewById(R.id.base_dialog_button_separator);
            Button cancelButton = (Button) rootView.findViewById(R.id.base_dialog_cancel_button);
            if (cancelButton != null) {
                cancelButton.setText(cancelButtonText);
                cancelButton.setOnClickListener(this);
                if (TextUtils.isEmpty(cancelButtonText)) {
                    cancelButton.setVisibility(View.GONE);
                    separatorView.setVisibility(View.GONE);
                }
            }
            Button confirmButton = (Button) rootView.findViewById(R.id.base_dialog_confirm_button);
            if (confirmButton != null) {
                confirmButton.setText(confirmButtonText);
                confirmButton.setOnClickListener(this);
                if (TextUtils.isEmpty(confirmButtonText)) {
                    confirmButton.setVisibility(View.GONE);
                    separatorView.setVisibility(View.GONE);
                }
            }
            // set content view
            setContentView(rootView);
            this.setCanceledOnTouchOutside(this.canceledOnTouchOutside);
            this.contentView = rootView;
        }
    
        /**
         * 调整dialog的尺寸
         * @param resize
         */
        protected void resizeDialog(float resize) {
            if (resize <= 0) {
                return;
            }
            Window window = this.getWindow();
            window.setGravity(Gravity.CENTER);
            this.show();
            WindowManager windowManager = ((Activity) this.context).getWindowManager();
            Display display = windowManager.getDefaultDisplay();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = (int) (display.getWidth() * resize);
            window.setAttributes(lp);
        }
    
        public Context getDialogContext() {
            return this.context;
        }
    
        public void setIsAutoDismiss(boolean value) {
            isAutoDismiss = value;
        }
    
        public View getContentView() {
            return this.contentView;
        }
    
        public void setCancelButtonClickListener(OnClickListener cancelButtonClickListener) {
            this.cancelButtonClickListener = cancelButtonClickListener;
        }
    
        public boolean isCanceledOnTouchOutside() {
            return canceledOnTouchOutside;
        }
    
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.base_dialog_cancel_button) {
                dismiss();
                if (this.cancelButtonClickListener != null) {
                    this.cancelButtonClickListener.onClick(BaseDialog.this, v.getId());
                }
            } else if (v.getId() == R.id.base_dialog_confirm_button) {
                if (isAutoDismiss) {
                    dismiss();
                }
                if (this.confirmButtonClickListener != null) {
                    this.confirmButtonClickListener.onClick(BaseDialog.this, v.getId());
                }
            }
        }
    
    }
    

    selector_common_dialog_cancel_button_click.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/shape_basic_dialog_cancel_pressed" android:state_pressed="true" />
        <item android:drawable="@drawable/shape_basic_dialog_cancel_pressed" android:state_selected="true" />
        <item android:drawable="@drawable/shape_basic_dialog_cancel_normal" />
        <!-- the selector for cancel button of base dialog-->
    </selector>
    

    selector_common_dialog_confirm_button_click.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/shape_basic_dialog_confirm_pressed" android:state_pressed="true" />
        <item android:drawable="@drawable/shape_basic_dialog_confirm_pressed" android:state_selected="true" />
        <item android:drawable="@drawable/shape_basic_dialog_confirm_normal" />
        <!-- the selector for cancel button of base dialog-->
    </selector>
    

    shape_basic_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="@android:color/white"/>
        <stroke android:color="@android:color/darker_gray" android:width="1dp" />
        <corners android:radius="5dp" />
        <!-- the shape for base dialog-->
    </shape>
    

    shape_basic_dialog_cancel_normal.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="#dddddd"/>
        <corners android:bottomLeftRadius="5dp" />
        <!-- the normal shape for cancel button of base dialog-->
    </shape>
    

    shape_basic_dialog_cancel_pressed.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="#ff0000"/>
        <corners android:bottomLeftRadius="5dp" />
        <!-- the pressed shape for cancel button of base dialog-->
    </shape>
    

    shape_basic_dialog_confirm_normal.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="#ff0000"/>
        <corners android:bottomLeftRadius="5dp" />
        <!-- the pressed shape for cancel button of base dialog-->
    </shape>
    

    shape_basic_dialog_confirm_pressed.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="#ff0000"/>
        <corners android:bottomLeftRadius="5dp" />
        <!-- the pressed shape for cancel button of base dialog-->
    </shape>
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    
    </LinearLayout>
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/base_dialog_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_basic_dialog"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:orientation="vertical">
    
        <LinearLayout
            android:id="@+id/base_dialog_title_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/base_dialog_title"
                android:textColor="#333333"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="100dp"
                android:ellipsize="end"
                android:gravity="center"
                android:maxLines="2"
                android:textSize="15sp"
                android:padding="10dp"
                android:text="标题" />
    
            <View style="@style/CommonHorizontalDividerLineStyle" />
        </LinearLayout>
    
        <FrameLayout
            android:id="@+id/base_dialog_content_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp">
    
            <View style="@style/CommonHorizontalDividerLineStyle" />
        </FrameLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/base_dialog_cancel_button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/selector_common_dialog_cancel_button_click"
                android:gravity="center"
                android:text="取消"
                android:textColor="#bbbbbb"
                android:textSize="15sp" />
    
            <View
                android:id="@+id/base_dialog_button_separator"
                style="@style/CommonVerticalDividerLineStyle"
                android:background="#eeeeee"
                android:layout_height="match_parent" />
    
            <Button
                android:id="@+id/base_dialog_confirm_button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/selector_common_dialog_confirm_button_click"
                android:gravity="center"
                android:text="确定"
                android:textColor="#df0001"
                android:textSize="15sp" />
        </LinearLayout>
    
    </LinearLayout>
    

    styles.xml

    <!-- 基本对话框的样式 -->
        <style name="BaseDialogStyle">
            <!-- 背景使用 -->
            <item name="android:backgroundDimEnabled">true</item>
            <!-- 模糊,背景的灰度 -->
            <item name="android:backgroundDimAmount">0.5</item>
            <!-- 背景透明 -->
            <item name="android:windowBackground">@android:color/transparent</item>
            <!-- 背景半透明 -->
            <item name="android:windowIsTranslucent">true</item>
            <!-- 边框 -->
            <item name="android:windowFrame">@null</item>
            <!-- 是否浮现在activity之上 -->
            <item name="android:windowIsFloating">true</item>
            <!-- 是否有标题 -->
            <item name="android:windowNoTitle">true</item>
        </style>
    
        <!-- 通用水平横线样式 -->
        <style name="CommonHorizontalDividerLineStyle">
            <item name="android:layout_width">match_parent</item>
            <item name="android:layout_height">0.5dp</item>
            <item name="android:background">#eeeeee</item>
        </style>
    
        <!-- 通用垂直竖线样式-->
        <style name="CommonVerticalDividerLineStyle">
            <item name="android:layout_width">1dp</item>
            <item name="android:layout_height">match_parent</item>
            <item name="android:background">#cccccc</item>
        </style>
    

    相关文章

      网友评论

          本文标题:点击button弹出对话框

          本文链接:https://www.haomeiwen.com/subject/syasxqtx.html