Copyright notice : This paper is about HaiyuKing Original article , Reprint please indicate the source !

Preface

Inherit BottomSheetDialog, Realize simple confirmation and cancellation dialog style .

design sketch

The code analysis

ConfirmCancelBottomSheetDialog Inherit BottomSheetDialog.

Use steps

One 、 Project organization chart

matters needing attention :

1、 After importing the class file, you need change Package name and re import R File path

2、 Values A file in a directory (strings.xml、dimens.xml、colors.xml etc. ), If there is , Then copy the contents , Don't cover the whole thing

Two 、 Import steps

stay APP Medium bundle.gradle Add the following code to the file , introduce design【 Version number follows appcompat-v7 Keep consistent 】

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"
}

take ConfirmCancelBottomSheetDialog Copy the package into the project 【 Be careful , You have to open it manually Java The document is renewed import R file 】

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 The confirmation cancel dialog box pops up at the bottom 【 The minimalist style , Only the title 、 Cancel 、 Make sure the two buttons 】
*/ public class ConfirmCancelBottomSheetDialog extends BottomSheetDialog {
private static final String TAG = ConfirmCancelBottomSheetDialog.class.getSimpleName(); private Context mContext; /** Listener for setting dialog content and style ( title 、 Button style , Including control hiding )*/
private DialogSetListener mDialogSetListener;
/** Three button click event listener */
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); // You can achieve the bottom outer margin effect in disguise
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);// Red rice 6pro adapter
// Set transparency
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); //========================== Initial presentation ==========================
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();
}
});
} /** Listener for setting dialog content and style ( title 、 Content 、 Button style , Including control hiding )*/
public static abstract interface DialogSetListener
{
/** Set title 、 Content 、 The text of the button and the display and hiding of the button
* @param title - Title Control 【 Default “ Tips ”】
* @param leftBtn - Left button control 【 Default “ determine ”】
* @param rightBtn - Right button control 【 Default “ Cancel ”】*/
public abstract void setDialog(TextView title, TextView leftBtn, TextView rightBtn);
} public void setDialogSetListener(DialogSetListener dialogSetListener) {
mDialogSetListener = dialogSetListener;
} /** Three button click event listener */
public static abstract interface DialogClickListener
{
/** Cancel button */
public abstract void leftClickListener();
/** OK button */
public abstract void rightClickListener();
} public void setDialogClickListener(DialogClickListener dialogClickListener) {
mDialogClickListener = dialogClickListener;
}
}

ConfirmCancelBottomSheetDialog.java

take confirm_cancel_bottomsheet_btn_bg.xml File copy to res/drawable Directory

take dialog_bottomsheet_confirm_cancel.xml File copy to res/layout Directory

thus ,ConfirmCancelBottomSheetDialog You can use it .

3、 ... and 、 Usage method

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(" Whether or not to delete ?");
}
}); delDialog.setDialogClickListener(new ConfirmCancelBottomSheetDialog.DialogClickListener() {
@Override
public void leftClickListener() {
Toast.makeText(MainActivity.this," Cancel ",Toast.LENGTH_SHORT).show();
} @Override
public void rightClickListener() {
Toast.makeText(MainActivity.this," confirm ",Toast.LENGTH_SHORT).show();
}
}); delDialog.show();
}
});
}
}

Confusing configuration

nothing

Reference material

Time and space are short

project demo Download address

https://github.com/haiyuKing/ConfirmCancelBottomSheetDialogDemo

ConfirmCancelBottomSheetDialog【 Confirm to cancel the bottom dialog 】 More articles about

  1. ConfirmCancelUtilDialog【 Confirm to cancel the dialog encapsulation class 】

    Copyright notice : This paper is about HaiyuKing Original article , Reprint please indicate the source ! Preface Encapsulation of the confirm cancel dialog . Premise : Call the Activity Must inherit FragmentActivity And its subclasses ( such as AppCompat ...

  2. ConfirmCancelDialog【 Confirm cancel dialog 】

    Copyright notice : This paper is about HaiyuKing Original article , Reprint please indicate the source ! Preface Encapsulation of the confirm cancel dialog . Premise : Call the Activity Must inherit FragmentActivity And its subclasses ( such as AppCompat ...

  3. AlertConfirmDialog【 be based on AlertDialog Confirm cancel dialog box for 】

    Copyright notice : This paper is about HaiyuKing Original article , Reprint please indicate the source ! Preface Confirm cancel dialog , be based on AlertDialog. Not based on DialogFragment. The button text can be changed according to the actual situation . design sketch The code analysis ...

  4. javaScript Delete event Pop up confirmation Cancel the dialog

    javaScript Delete event Pop up confirmation Cancel the dialog 1. <a href="javascript:if(confirm(' Are you sure you want to delete ?'))location='http://www. ...

  5. Selenium series ( Nine ) - in the light of alert Window processing ( Warning box 、 Confirmation box 、 Dialog box )

    If you want to learn from scratch Selenium, Take a look at this series of articles ! https://www.cnblogs.com/poloyy/category/1680176.html secondly , If you don't know the front-end Basics , ...

  6. ThinkPHP Day 21 (JQuery Element acquisition ,parents attribute ,toggle Method , Confirm that the pop-up dialog box uses )

    1.JQuery usage A:$(function(){code...}); It means to execute when the page is loaded B: Get element method : according to class name $('.classname'), according to ID name $('#IDnam ...

  7. NewBuiltBottomSheetDialog【 New bottom dialog 】

    Copyright notice : This paper is about HaiyuKing Original article , Reprint please indicate the source ! Preface Show how to pop up the bottom dialog above the bottom tab . design sketch The code analysis NewBuiltBottomSheetDialog Inherit BottomShe ...

  8. js Pop up confirmation Cancel the dialog

    A kind of : <a href="javascript:if(confirm(' Are you sure you want to delete this content ?')){location='http://www.google.com'}"> ...

  9. javascript window.confirm confirm Cancel the dialog box to realize the code summary

    The three types described in this article are all based on javascript confirm Prompt confirmation box , Just write in different places , Students in need can refer to   confirm() Method confirm() Method is used to display an image with a finger ...

Random recommendation

  1. iOS The use of development XMPPFramework Instant messaging ( One )

    About XMPP In this blog, I won't repeat the introduction of the theory , How to join in our previous wechat XMPP Protocol to realize communication ? I'll talk about XMPP Basic knowledge of , Let our wechat realize Internet Communication . The preparation is to have server support X ...

  2. Scrum Get the task

    This time, we mainly discussed the product construction process , How to assign tasks to individuals , Discuss what functions and how to implement them , And then they chose their own tasks . In team projects “ Guangshang department store ” Of SCRUM The task I claim in the project is to realize the login function . It's not officially started yet , Still reading and ...

  3. jqurey Cross domain use of getjson(http://www.jb51.net/Special/894.htm)

    JS Is a cross-domain problem , I think many programmers think that JS It's not cross domain , In fact, this is a wrong view : There are a lot of people looking for solutions online , Teach it to use IFRAME There are many articles to solve , Is it really that complicated ? It's very simple , If you use JQUER ...

  4. The second assignment ——C++ Study

    Course selection : In the past, I paid more attention to some self-study websites in the process of self-study , So this time "C++ self-taught " I think it's easy to find a course . Because before the web and other learning are in MOOCS ( Video learning is a time-consuming experience , Especially sometimes with ...

  5. cocos2d-x Game pause interface , monitor home key , Return key ,Menu key Solution

    Game pause interface : cocos2d-x The idea provided by the game pause interface is to use pushScene() and popScne(), Push and pop up the scene , When the game stops , advance (pushScene()) Pause the scene , The previously run scenario will automatically ...

  6. ASP.NET MVC 4 Let the database migrate automatically

    I actually tested this method today , You can keep the database in sync with the entity class , At the same time, the prompt of database migration will not appear . But you can only change the entity class to change the database , You can't change the database to change the entity class . So this is Code frist, If you change it by changing the database table ...

  7. android Write file permissions

    First , stay manifest.xml Add user permission:<uses-permission android:name="android.permission.WRITE_E ...

  8. Desktop widget Wight Parent class AppWidgetProvider Three methods of

    onUpdate() This method will be updated every time App Widget Called when , The logic of data update is written in this method . And pay attention to : When users add widgets , This method will be called first , Initialization should be done in this method , Than ...

  9. centos7+apache+svn To configure Step on the pit , Pay attention to the problem of authority .apache Application directory checkout application You have to use this command :svn co file:///home/svn/test/ test , adopt svn add * &amp;&amp;commit It's impossible to make any changes

    Reading help Command prompt [root@server-002 ~]# Represents the current service root Commands executed by the user [svn@server-002 ~]$ For ordinary users svn Executed command [root@localhost ...

  10. Java Simple application of anonymous inner class

    When looking at digital photos , You usually use an image viewing software , The software should be able to traverse all the pictures under the folder and display them . Programming , Implementation of a picture view software , It can support 6 A picture , You can see different pictures by clicking different buttons . Thought analysis : Namely ...