Android如何实现年月选择器功能

开发过程中,年月的选择功能还是比较常见的,像这种功能点比较常见,要是每次都要自己手动去写,这无疑会耗费比较多的时间与精力,今天给大家介绍一个第三方库,使用该库来完成年月选择器功能。

一、效果图

二、实现步骤:

1、依赖库

implementation 'cn.aigestudio.wheelpicker:WheelPicker:1.1.3'

2、xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="200dp"
 android:background="#ffffff">

 <TextView
  android:id="@+id/cancel"
  android:layout_width="60dp"
  android:layout_height="40dp"
  android:gravity="center"
  android:text="取消"
  android:textColor="#666666"
  android:textSize="17sp"
  app:layout_constraintLeft_toLeftOf="parent"
  app:layout_constraintTop_toTopOf="parent" />

 <TextView
  android:id="@+id/ok"
  android:layout_width="60dp"
  android:layout_height="40dp"
  android:gravity="center"
  android:text="确定"
  android:textColor="#3C76FF"
  android:textSize="17sp"
  app:layout_constraintRight_toRightOf="parent"
  app:layout_constraintTop_toTopOf="parent" />

 <View
  android:id="@+id/view_line"
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="#e5e5e5"
  app:layout_constraintLeft_toLeftOf="parent"
  app:layout_constraintTop_toBottomOf="@id/cancel" />

 <com.aigestudio.wheelpicker.WheelPicker
  android:id="@+id/mWheelPicker_1"
  android:layout_width="0dp"
  android:layout_height="0dp"
  android:layout_marginLeft="30dp"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintLeft_toLeftOf="parent"
  app:layout_constraintRight_toLeftOf="@id/mWheelPicker_2"
  app:layout_constraintTop_toBottomOf="@id/view_line"
  app:wheel_atmospheric="true"
  app:wheel_curtain_color="#1886F7"
  app:wheel_curved="true"
  app:wheel_cyclic="true"
  app:wheel_indicator_color="#e5e5e5"
  app:wheel_item_text_color="#919191"
  app:wheel_item_text_size="23sp"
  app:wheel_selected_item_text_color="#000000" />

 <com.aigestudio.wheelpicker.WheelPicker
  android:id="@+id/mWheelPicker_2"
  android:layout_width="0dp"
  android:layout_height="0dp"
  android:layout_marginRight="30dp"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintLeft_toRightOf="@id/mWheelPicker_1"
  app:layout_constraintRight_toRightOf="parent"
  app:layout_constraintTop_toTopOf="@id/mWheelPicker_1"
  app:wheel_atmospheric="true"
  app:wheel_curtain_color="#1886F7"
  app:wheel_curved="true"
  app:wheel_cyclic="true"
  app:wheel_indicator_color="#e5e5e5"
  app:wheel_indicator_size="24sp"
  app:wheel_item_text_color="#919191"
  app:wheel_item_text_size="23sp"
  app:wheel_selected_item_text_color="#000000" />

</android.support.constraint.ConstraintLayout>

3、添加数据

 List<String> CEOYEAR = new ArrayList<>();
 List<String> CEOMONTH = new ArrayList<>();

 for (int i = 2000; i < 2051; i++) {
     CEOYEAR.add(i + "");
  }

 for (int i = 1; i < 13; i++) {
  CEOMONTH.add(i + "");
 }

4、设置选择器弹出框

 /**
  * @desc : 两个滚动器
  **/
 private void showTwoWheelPicker(Context context, final List<String> data1, final List<String> data2, final TwoWheelListener mTwoWheelListener) {

  final Dialog dialog = getDialog(context);
  Window window = dialog.getWindow();
  window.setGravity(Gravity.BOTTOM);
  window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  window.setContentView(R.layout.fragment_sami);

  final WheelPicker wv1 = window.findViewById(R.id.mWheelPicker_1);
  final WheelPicker wv2 = window.findViewById(R.id.mWheelPicker_2);

  wv1.setData(data1);
  wv2.setData(data2);

  //取消
  window.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    dialog.dismiss();
   }
  });

  //确定
  window.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    dialog.dismiss();

    if (mTwoWheelListener != null) {
     mTwoWheelListener.onOk(data1.get(wv1.getCurrentItemPosition()), data2.get(wv2.getCurrentItemPosition()));
    }
   }
  });
 }

 private Dialog getDialog(Context context) {

  return new AlertDialog.Builder(context, R.style.RoundCornerDialog).setCancelable(false).show();
 }

 private TwoWheelListener mTwoWheelListener = null;

 public static interface TwoWheelListener {
  void onOk(String str1, String str2);
 }

5、设置弹出框dialog样式

 <!--圆角的dialog样式-->
 <style name="RoundCornerDialog" parent="@android:style/Theme.Dialog">
  <item name="android:windowFrame">@null</item>
  <item name="android:windowIsFloating">true</item>
  <item name="android:windowIsTranslucent">true</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:background">@android:color/transparent</item>
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:backgroundDimEnabled">true</item>
  <item name="android:backgroundDimAmount">0.6</item>
</style>

6、设置点击事件弹出

 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    showTwoWheelPicker(AppBarLayoutActivity.this, CEOYEAR, CEOMONTH, new TwoWheelListener() {
     @Override
     public void onOk(String str1, String str2) {
      Toast.makeText(AppBarLayoutActivity.this, str1 + "年" + str2 + "日", Toast.LENGTH_SHORT).show();
     }
    });
   }
  });

四、总结

这个第三方库我这里只是做了简单的介绍,还有更多需求的还是去阅读第三方库。

第三方库地址:

https://github.com/AigeStudio/WheelPicker

到这里就结束啦。

以上就是Android如何实现年月选择器功能的详细内容,更多关于Android实现年月选择器功能的资料请关注我们其它相关文章!

(0)

相关推荐

  • Android日期选择器实现年月日三级联动

    最近项目里面用到了一个日期选择器,实现现在主流的WheelView滑动选择,整理了下,做了个Demo.废话不多说,直接上代码. 主布局:activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&quo

  • android实现双日期选择控件(可隐藏日,只显示年月)

    在安卓开发中,会碰到选开始日期和结束日期的问题.特别是在使用Pad时,如果弹出一个Dialog,能够同时选择开始日期和结束日期,那将是极好的.我在开发中在DatePickerDialog的基础上做了修改,实现了这种Dialog.效果如下: 具体实现方法为: 先新建一个安卓项目DoubleDatePicker,在res/layout文件夹下新建date_picker_dialog.xml,内容如下: <?xml version="1.0" encoding="utf-8&

  • Android动态显示当前年月日时分秒系统时间(示例代码)

    在布局文件中放一个TextView用来显示时间,如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="

  • Android自定义dialog可选择展示年月日时间选择栏

    自定义dialog package com.poptest; import android.app.DatePickerDialog; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.DatePicker; //dialog类 public class YearPickerDialog extends DatePickerD

  • Android 用Time和Calendar获取系统当前时间源码分享(年月日时分秒周几)

    概述 用Time和Calendar获取系统当前时间(年月日时分秒周几) 效果图 源码: import android.app.Activity; import android.os.Bundle; import android.text.format.Time; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.Calen

  • Android如何实现年月选择器功能

    开发过程中,年月的选择功能还是比较常见的,像这种功能点比较常见,要是每次都要自己手动去写,这无疑会耗费比较多的时间与精力,今天给大家介绍一个第三方库,使用该库来完成年月选择器功能. 一.效果图 二.实现步骤: 1.依赖库 implementation 'cn.aigestudio.wheelpicker:WheelPicker:1.1.3' 2.xml布局文件 <?xml version="1.0" encoding="utf-8"?> <andr

  • Android 中TeaPickerView数据级联选择器功能的实例代码

    Github地址 YangsBryant/TeaPickerView (Github排版比较好,建议进入这里查看详情,如果觉得好,点个star吧!) 引入module allprojects { repositories { google() jcenter() maven { url 'https://www.jitpack.io' } } } implementation 'com.github.YangsBryant:TeaPickerView:1.0.2' 主要代码 public cla

  • Android实现图片选择器功能

    本文实例为大家分享了Android实现图片选择器功能的具体代码,供大家参考,具体内容如下 图片选择功能用的是GitHub上的依赖库,网址 先来看下我运行的效果图如下所示: 该依赖库是Android平台上拍照/录像,图片/视频选择,编辑和压缩的一站式解决方案. 添加依赖,在app->build.gradle里面添加依赖 //图片/视频选择.预览.编辑与拍照 implementation 'com.github.guoxiaoxing:phoenix:1.0.15' 初始化: public clas

  • JS仿JQuery选择器功能

    JQuery作为应用最广的JS库,其最强大的功能之一就是几乎涵盖所有方法的且代码十分简短的选择器功能,我们也可用自己的代码实现此功能,代码逻辑.使用方法与JQuery一致 function ZQuery(arg){ this.elements = []; //存东西 this.domString = ''; //保存字符串标签 if(typeof arg=='function'){ //DOMReady DOMReady(arg); }else if(typeof arg=='string'||

  • Android实现二级列表购物车功能

    本文实例为大家分享了Android实现二级列表购物车功能的具体代码,供大家参考,具体内容如下 MainActivity: package com.baway.twoshopcar; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.Expa

  • Android仿微信语音聊天功能

    本文实例讲述了Android仿微信语音聊天功能代码.分享给大家供大家参考.具体如下: 项目效果如下: 具体代码如下: AudioManager.java package com.xuliugen.weichat; import java.io.File; import java.io.IOException; import java.util.UUID; import android.media.MediaRecorder; public class AudioManager { private

  • Android AndBase框架实现多功能标题栏(一)

    本文是针对AndBase框架学习整理的第一篇笔记,想要了解AndBase框架的朋友可以阅读本文,大家共同学习. 1.使用AndBase实现多功能标题栏 AndBase框架内部提供了许多的方式能够使我们去设置一个更好的标题栏,进行动态的改变,而并非静态的将标题栏界面写死...能够使得标题栏更加的美观...总体就是动态的获取布局然后通过对布局的操作来自定义一个良好的标题栏... 使用AndBase框架的时候我们的主函数就不是继承于Acticity了,而是继承于AbActivity,万变不离其宗还是A

  • Android Studio 3.0 新功能全面解析和旧项目适配问题

    简介: Android Studio是Android的官方IDE.它是专为Android而打造,可以加快您的开发速度,帮助您为每款Android设备构建最优应用. 它提供专为Android开发者量身定制的工具,其中包括丰富的代码编辑.调试.测试和性能分析工具. 上周四,Google 终于在经历大半年的打磨锤炼之后正式发布 Android Studio 3.0 版本,给广大安卓开发人员一份满意的答卷.如往常一样,每次新版开发工具的发布,很多谨慎点的朋友仍担心稳定性.是否存在坑等问题,选择隔岸观火,

  • Android实现截图和分享功能的代码

    先给大家展示下效果图吧 直接上代码: xml的布局: <Button android:id="@+id/btn_jp" android:layout_marginTop="10dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:tex

  • Android实现底部导航栏功能(选项卡)

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片.直接上各个布局文件或各个类的代码: 1. res/layout目录下的 maintabs.xml 源码: <?xml version="1.0&q

随机推荐