ConfirmCancelBottomSheetDialog【确认取消底部对话框】
版权声明:本文为HaiyuKing原创文章,转载请注明出处!
前言
继承BottomSheetDialog,实现简单的确认取消对话框样式。
效果图
代码分析
ConfirmCancelBottomSheetDialog继承BottomSheetDialog。
使用步骤
一、项目组织结构图
注意事项:
1、 导入类文件后需要change包名以及重新import R文件路径
2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
二、导入步骤
在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; } }
将confirm_cancel_bottomsheet_btn_bg.xml文件复制到res/drawable目录中
将dialog_bottomsheet_confirm_cancel.xml文件复制到res/layout目录中
至此,ConfirmCancelBottomSheetDialog就可以使用了。
三、使用方法
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(); } }); } }
混淆配置
无
参考资料
暂时空缺
项目demo下载地址
https://github.com/haiyuKing/ConfirmCancelBottomSheetDialogDemo