Android 基于NumberPicker自定义弹出窗口Dialog整合日期选择器

Android实现把年月选择器放到AlertDialog中_左眼看成爱的博客-CSDN博客

Android使用NumberPicker实现年月滚动选择器_左眼看成爱的博客-CSDN博客

前面两篇文章我们分别讲了

1,如何用NumberPicker实现年月选择器

2,如何把1中的用NumberPicker实现的年月选择器放到AlertDialog中去。

这篇文章我们则推出实现了弹层遮罩联动的最终版本,具体实现的效果请看示例图:

上面两个角圆角化:

 

 1 定义自定义的Dialog样式

在res/values/styles.xml文件中定义自定义的Dialog样式:

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

其中,BottomDialog是自定义的Dialog样式名称,windowIsFloating设置为false表示Dialog不浮在activity之上,windowNoTitle表示Dialog没有标题栏,windowBackground设置为透明色,windowAnimations设置为Dialog的进出动画,background设置为透明色,windowSoftInputMode设置为stateUnspecified|adjustResize表示弹出Dialog时不会挤压当前界面的布局,windowIsTranslucent设置为true表示Dialog的背景为透明,windowContentOverlay设置为null表示Dialog的内容不会覆盖系统状态栏。

2 创建Dialog对象

在Java代码中创建Dialog对象,并设置样式:

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

其中,setContentView方法设置Dialog的布局文件,setCancelable方法设置是否可以通过返回键关闭Dialog,setCanceledOnTouchOutside方法设置是否可以通过点击Dialog外部关闭Dialog。 

3 显示Dialog

dialog.show();

将上面那两篇教程中的布局文件复制过来即可,也就是R.layout.your_layout

另附上:上面两个角圆角化的代码:

<?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>

 原创文章,转载请注明出处

猜你喜欢

转载自blog.csdn.net/wh445306/article/details/130350335