Android integrates date picker based on NumberPicker custom pop-up window Dialog

Android implementation puts the year and month selector in AlertDialog - Programmer Sought

Android uses NumberPicker to implement the year and month scrolling selector - Programmer Sought

In the previous two articles, we talked about

1. How to implement the year and month selector with NumberPicker

2. How to put the year and month selector implemented with NumberPicker in 1 into AlertDialog.

In this article, we launched the final version that realizes the linkage of the pop-up layer mask. Please see the example picture for the specific implementation effect:

The upper two corners are rounded:

 

 

 1 Define a custom Dialog style

Define custom Dialog styles in the res/values/styles.xml file:

<style name="BottomDialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowAnimations">@style/DialogAnimation</item>
    <item name="android:background">@color/transparent</item>
 <!--    <item name="android:windowSoftInputMode">stateUnspecified|adjustResize</item>-->
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_in_up</item>
    <item name="android:windowExitAnimation">@anim/slide_out_down</item>
</style>

Among them, BottomDialog is a custom Dialog style name, windowIsFloating is set to false to indicate that Dialog does not float above the activity, windowNoTitle indicates that Dialog has no title bar, windowBackground is set to transparent color, windowAnimations is set to Dialog's in and out animation, and background is set to transparent color , windowSoftInputMode is set to stateUnspecified|adjustResize means that the layout of the current interface will not be squeezed when the Dialog is popped up, windowIsTranslucent is set to true means the background of the Dialog is transparent, windowContentOverlay is set to null means the content of the Dialog will not cover the system status bar.

2 Create a Dialog object

Create a Dialog object in Java code and set the style:

Dialog dialog = new Dialog(context, R.style.BottomDialog);
dialog.setContentView(R.layout.your_layout);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);

Among them, the setContentView method sets the layout file of the Dialog, the setCancelable method sets whether the Dialog can be closed by the return key, and the setCanceledOnTouchOutside method sets whether the Dialog can be closed by clicking outside the Dialog. 

3 Display Dialog

dialog.show();

Copy the layout files in the above two tutorials, that is, R.layout.your_layout

Attached: the code for rounding the above two corners:

<?xml version="1.0" encoding="utf-8"?>
<!-- arc_shape.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

   <!-- 用于绘制上部分的两个角弧形-->
    <corners
            android:topLeftRadius="15dp"
            android:topRightRadius="15dp" />

    <!-- 填充的颜色 -->
    <solid android:color="@android:color/white" />

</shape>

 Original article, reprint please indicate the source

Guess you like

Origin blog.csdn.net/wh445306/article/details/130350335