ConfirmCancelBottomSheetDialog【確認取消底部對話框】

版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!java

前言

繼承BottomSheetDialog,實現簡單的確認取消對話框樣式。android

效果圖

代碼分析

ConfirmCancelBottomSheetDialog繼承BottomSheetDialoggit

使用步驟

1、項目組織結構圖

注意事項:github

一、 導入類文件後須要change包名以及從新import R文件路徑app

二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋ide

2、導入步驟

在APP中的bundle.gradle文件中添加如下代碼,引入design【版本號跟appcompat-v7的保持一致

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.why.project.confirmcancelbottomsheetdialogdemo"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //BottomSheetDialog compile "com.android.support:design:27.1.1"
}

將ConfirmCancelBottomSheetDialog包複製到項目中【注意,須要手動打開Java文件從新import R文件】

package com.why.project.confirmcancelbottomsheetdialogdemo.confirmcancel;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.why.project.confirmcancelbottomsheetdialogdemo.R;


/**
 * Used 底部彈出來的確認取消對話框【簡約版的樣式,只有標題、取消、肯定兩個按鈕】
 */

public class ConfirmCancelBottomSheetDialog extends BottomSheetDialog {
    private static final String TAG = ConfirmCancelBottomSheetDialog.class.getSimpleName();

    private Context mContext;

    /**設置對話框內容和樣式的監聽器(標題、按鈕樣式,包括控制隱藏)*/
    private DialogSetListener mDialogSetListener;
    /**三個按鈕的點擊事件監聽器*/
    private DialogClickListener mDialogClickListener;

    private TextView mTitle;
    private TextView mLeftBtn;
    private TextView mRightBtn;

    public ConfirmCancelBottomSheetDialog(@NonNull Context context, DialogSetListener dialogSetListener) {
        super(context);
        mContext = context;
        mDialogSetListener = dialogSetListener;
    }

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

        //能夠變相實現底部外邊距效果
        int screenHeight = getScreenHeight((Activity) mContext);
        int statusBarHeight = getStatusBarHeight(getContext());
        int dialogHeight = screenHeight;//
//        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//紅米6pro適配
        //設置透明
        getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);

        initViews();
        initEvents();
    }

    private static int getScreenHeight(Activity activity) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics.heightPixels;
    }

    private static int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

    private void initViews() {

        mTitle = findViewById(R.id.confirm_title);
        mLeftBtn = findViewById(R.id.left_btn);
        mRightBtn = findViewById(R.id.right_btn);

        //==========================初始展示==========================
        if(mDialogSetListener != null){
            mDialogSetListener.setDialog(mTitle, mLeftBtn, mRightBtn);
        }

    }

    private void initEvents() {
        mLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.leftClickListener();
                }
                dismiss();
            }
        });

        mRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.rightClickListener();
                }
                dismiss();
            }
        });
    }

    /**設置對話框內容和樣式的監聽器(標題、內容、按鈕樣式,包括控制隱藏)*/
    public static abstract interface DialogSetListener
    {
        /**設置標題、內容、按鈕的文本以及按鈕的顯示隱藏
         * @param title - 標題控件【默認「提示」】
         * @param leftBtn - 左側按鈕控件【默認「肯定」】
         * @param rightBtn - 右側按鈕控件【默認「取消」】*/
        public abstract void setDialog(TextView title, TextView leftBtn, TextView rightBtn);
    }

    public void setDialogSetListener(DialogSetListener dialogSetListener) {
        mDialogSetListener = dialogSetListener;
    }

    /**三個按鈕的點擊事件監聽器*/
    public static abstract interface DialogClickListener
    {
        /**取消按鈕*/
        public abstract void leftClickListener();
        /**肯定按鈕*/
        public abstract void rightClickListener();
    }

    public void setDialogClickListener(DialogClickListener dialogClickListener) {
        mDialogClickListener = dialogClickListener;
    }
}
ConfirmCancelBottomSheetDialog.java

將confirm_cancel_bottomsheet_btn_bg.xml文件複製到res/drawable目錄中

 

將dialog_bottomsheet_confirm_cancel.xml文件複製到res/layout目錄中

 

至此,ConfirmCancelBottomSheetDialog就能夠使用了。gradle

3、使用方法

package com.why.project.confirmcancelbottomsheetdialogdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.why.project.confirmcancelbottomsheetdialogdemo.confirmcancel.ConfirmCancelBottomSheetDialog;

public class MainActivity extends AppCompatActivity {

    private Button btn_open;

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

        initViews();
        initEvents();
    }

    private void initViews() {
        btn_open = findViewById(R.id.btn_open);
    }

    private void initEvents() {
        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ConfirmCancelBottomSheetDialog delDialog = new ConfirmCancelBottomSheetDialog(MainActivity.this, new ConfirmCancelBottomSheetDialog.DialogSetListener() { @Override public void setDialog(TextView title, TextView leftBtn, TextView rightBtn) { title.setText("是否刪除?"); } }); delDialog.setDialogClickListener(new ConfirmCancelBottomSheetDialog.DialogClickListener() { @Override public void leftClickListener() { Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show(); } @Override public void rightClickListener() { Toast.makeText(MainActivity.this,"確認",Toast.LENGTH_SHORT).show(); } }); delDialog.show();
            }
        });
    }
}

混淆配置

ui

參考資料

暫時空缺this

項目demo下載地址

https://github.com/haiyuKing/ConfirmCancelBottomSheetDialogDemospa

相關文章
相關標籤/搜索