首页 > 其他分享 >基于WheelView自定义的DatePickerDialog

基于WheelView自定义的DatePickerDialog

时间:2023-04-06 22:04:49浏览次数:56  
标签:case String 自定义 DatePickerDialog WheelView month year date btn


本人利用WheelView写的一个DatePickerDialog


(还有一个TimePickerDialog,本人忘了在写在哪个项目里了,等找到了也贴上来)



先看图,有个直观的了解



基于WheelView自定义的DatePickerDialog_ide




DatePickerDialog代码:

import java.util.Calendar;

import com.widget.wheel.NumericWheelAdapter;
import com.widget.wheel.OnWheelScrollListener;
import com.widget.wheel.WheelView;
import com.yirui.youbao.app.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * 
 * @author pythoner
 * 
 */
public class DatePickerDialog extends Dialog
{

    private Button btn_left, btn_right;

    private WheelView year, month, day;

    private String date;//初始化显示的日期,默认为当日

    public DatePickerDialog(Context context, String date)
    {
//        super(context);
        this(context, R.style.Theme_Dialog_NoTitle, date);
        // TODO Auto-generated constructor stub
    }

    public DatePickerDialog(Context context, int theme, String date)
    {
        super(context, theme);
        // TODO Auto-generated constructor stub
        this.date = date;
        init();
    }

    private void init()
    {
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_date_picker);

        initViews();
        initValues();
    }

    private void initViews()
    {
        btn_left = (Button) findViewById(R.id.btn_left);
        btn_left.setOnClickListener(clickListener);
        btn_right = (Button) findViewById(R.id.btn_right);
        btn_right.setOnClickListener(clickListener);

        String icurYear, icurMonth, icurDay;
        Calendar c = Calendar.getInstance();
        int curYear = c.get(Calendar.YEAR);
        if (date == null || date.length() < 10)
        {// 格式不正确
            int curMonth = c.get(Calendar.MONTH);
            int curDay = c.get(Calendar.DAY_OF_MONTH);
            icurYear = String.valueOf(curYear);
            icurMonth = String.valueOf(curMonth + 1);
            icurDay = String.valueOf(curDay);
        }
        else
        {// 年月日
            icurYear = date.substring(0, 4);
            icurMonth = date.substring(5, 7);
            icurDay = date.substring(8, 10);
        }

        year = (WheelView) findViewById(R.id.year);
        year.setAdapter(new NumericWheelAdapter(curYear - 2, curYear));
        // year.setLabel("年");
        year.setCurrentItem(Integer.parseInt(icurYear) - Integer.parseInt(year.getAdapter().getItem(0)));

        month = (WheelView) findViewById(R.id.month);
        month.setAdapter(new NumericWheelAdapter(1, 12));// "%02d"
        // month.setLabel("月");
        month.setCyclic(true);
        month.setCurrentItem(Integer.parseInt(icurMonth) - 1);

        int daysOfMounth = getDaysOfMounth();
        day = (WheelView) findViewById(R.id.day);
        day.setAdapter(new NumericWheelAdapter(1, daysOfMounth));
        // day.setLabel("日");
        day.setCyclic(true);
        day.setCurrentItem(Integer.parseInt(icurDay) - 1);

        OnWheelScrollListener scrollListener = new OnWheelScrollListener()
        {
            public void onScrollingStarted(WheelView wheel)
            {
            }

            public void onScrollingFinished(WheelView wheel)
            {
                int daysOfMounth = getDaysOfMounth();
                day.setAdapter(new NumericWheelAdapter(1, daysOfMounth));
            }
        };

        year.addScrollingListener(scrollListener);
        month.addScrollingListener(scrollListener);
        // day.addScrollingListener(scrollListener);

    }

    private int getDaysOfMounth()
    {
        int mMonth = Integer.parseInt(month.getAdapter().getItem(month.getCurrentItem()));
        switch (mMonth)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                int mYear = Integer.parseInt(year.getAdapter().getItem(year.getCurrentItem()));
                if (mYear % 4 == 0 && mYear % 100 != 0 || mYear % 400 == 0)
                {
                    return 29;
                }
                else
                {
                    return 28;
                }
        }
        return -1;
    }

    private void initValues()
    {

    }

    View.OnClickListener clickListener = new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            switch (v.getId())
            {
                case R.id.btn_left:
                    dismiss();
                    break;
                case R.id.btn_right:
                    String mYear = year.getAdapter().getItem(year.getCurrentItem());
                    String mMonth = month.getAdapter().getItem(month.getCurrentItem());
                    String mDay = day.getAdapter().getItem(day.getCurrentItem());
                    if (mDay == null || mDay.length() == 0)
                    {
                        mDay = "1";
                    }
                    if (Integer.parseInt(mMonth) < 10)
                    {
                        mMonth = "0" + mMonth;
                    }
                    if (Integer.parseInt(mDay) < 10)
                    {
                        mDay = "0" + mDay;
                    }
                    if (onOKClickListener != null)
                    {
                        onOKClickListener.onOKClick(mYear, mMonth, mDay);
                    }
                    dismiss();
                    break;

                default:
                    break;
            }
        }

    };

    private OnOKClickListener onOKClickListener;

    public interface OnOKClickListener
    {
        public void onOKClick(String year, String month, String date);
    }

    public void setOnOKClickListener(OnOKClickListener onOKClickListener)
    {
        this.onOKClickListener = onOKClickListener;
    }
}




DatePickerDialog的布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:gravity="center"
    android:background="@drawable/bg_picker_dialog"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="8dp" 
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:layout_weight="1" 
            android:gravity="center_horizontal"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />

            <com.widget.wheel.WheelView
                android:id="@+id/year"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="10dip"
                android:layout_marginTop="10dip"
                android:layout_weight="1" 
                />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:layout_weight="1" 
            android:gravity="center_horizontal"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />

            <com.widget.wheel.WheelView
                android:id="@+id/month"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="8dip"
                android:layout_marginTop="8dip"
                android:layout_weight="1" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />
        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:layout_weight="1" 
            android:gravity="center_horizontal"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />

            <com.widget.wheel.WheelView
                android:id="@+id/day"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="8dip"
                android:layout_marginTop="8dip"
                android:layout_weight="1" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />
        </LinearLayout>
        
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        android:background="@drawable/line_top_with_blue"
        >

        <Button
            android:id="@+id/btn_left"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="8dip"
            android:paddingTop="8dip"
            android:text="@string/cancel"
            android:textColor="@android:color/black"
            android:textSize="@dimen/font_big" 
            android:background="@drawable/bg_btn_trans_blue_with_no_corner"
            />


        <View 
            android:layout_width="@dimen/line_width"
            android:layout_height="match_parent"
            android:background="@color/primary"
            />
        
        <Button
            android:id="@+id/btn_right"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="8dip"
            android:paddingTop="8dip"
            android:text="@string/confirm"
            android:textColor="@android:color/black"
            android:textSize="@dimen/font_big" 
            android:background="@drawable/bg_btn_trans_blue_with_no_corner"
            />
    </LinearLayout>

</LinearLayout>




style:


<style name="Theme_Dialog_NoTitle" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>





使用:


dialog = new DatePickerDialog(context, R.style.Theme_Dialog, btn_startDate.getText().toString().trim());
			dialog.setOnOKClickListener(new DatePickerDialog.OnOKClickListener()
            {

                @Override
                public void onOKClick(String year, String month, String date)
                {
                    // TODO Auto-generated method stub
                    //dosomething
                    //btn_startDate.setText(year + "-" + month + "-" + date);
                   
                }
            });
            dialog.show();




WheelView类见附件,直接加入到你的工程中即可




安卓选择器类库,包括日期时间选择器等:AndroidPicker


http://www.open-open.com/lib/view/open1445774842960.html

  • 基于WheelView自定义的DatePickerDialog_ide_02

  • 大小: 23.1 KB
  • wheel.rar (10.1 KB)
  • 下载次数: 40
  • 查看图片附件

标签:case,String,自定义,DatePickerDialog,WheelView,month,year,date,btn
From: https://blog.51cto.com/u_5454003/6174218

相关文章

  • 自定义View实现HTML图文环绕效果
    Android中并没有提供HTML图文环绕效果的View,最接近的算是TextView中的ImageSpan了,但并未完全实现图文环绕(图文混排)的效果。1、Android系统TextView的ImageSpan实现图文环绕代码如下:TextViewtv=newTextView(this);SpannableStringspanStr=n......
  • 自定义竖直旋转显示文字的TextView
    先看效果(最右边的Buttons):原理很简单,就是使用了drawTextOnPath()沿着一条垂直的直线绘制文字,该直线可以从上往下或者从下往上,通过direction属性控制文字显示的方向。该类是本人要处理垂直显示英文字的时候逼出来的,呵呵;如果是中文字就简单了,直接加个换行符就满......
  • 【C】自定义类型(二)位段,枚举,联合
    前一章我们介绍了结构体,这一章我们来介绍一下内容:结构体实现位段(位段的填充&可移植性)枚举枚举类型的定义枚举的优点枚举的使用联合联合类型的定义联合的特点联合大小的计算1.位段结构体学完我们就得拥有结构体实现位段的能力。1.1什么是位段位段的声明和结构是类似的,有两个不同......
  • 题目 1027: [编程入门]自定义函数处理最大公约数与最小公倍数
    题目描述写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果两个整数由键盘输入。输入格式两个数输出格式最大公约数最小公倍数样例输入复制615样例输出复制330解题思路:欧几里得算法又称辗转相除法,用来求两......
  • 题目 1028: [编程入门]自定义函数求一元二次方程
    题目描述求方程的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。输入格式abc输出格式x1=?x2=?样例输入复制411样例输出复制x1=-0.125+0.484ix2=-0.125-0.484i解题思路:一元二次方程只含有......
  • 50、K8S-自定义资源定义-CustomResourceDefinition
    Kubernetes学习目录1、基础知识1.1、回顾到目前位置,我们为了在k8s上能够正常的运行我们所需要的服务,需要遵循以下方式来创建相关资源:1、合理的分析业务需求。2、梳理业务需求的相关功能。3、定制不同功能的资源配置文件。4、应用资源配置文件,完善业务环境。1.2、需求......
  • Android自定义捕获Application全局异常
    参考:http://bigcat.easymorse.com/?p=1152packageqianlong.qlmobile.ui;importjava.io.File;importjava.io.FileOutputStream;importjava.io.FilenameFilter;importjava.io.PrintWriter;importjava.io.StringWriter;importjava.io.Writer......
  • 微信公众号开发--获取网页授权并自定义菜单点击获得openid跳转
    之前做了个自定义菜单的click事件,但是订阅号升级为服务号,相应的有了网页的基础授权(需要认证的服务号)其中使用到了自定义菜单接口:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html网页授权获取用户基本信息接口:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0......
  • C# List按照自定义的顺序去排序
    有没有遇到过产品经理说表格输出排序要按照指定的人员列表去排序?经过一番研究搜查发现一个方法可以实现话不多说上例子 publicclassUserInfo{publicstringName{get;set;}publicstringInfo{get;set;}} List<UserInfo>userInfos=newList<Use......
  • [K/3Cloud] 首页增加一个自定义页签及页面内容
    在K3Cloud登录后的门户首页增加一个页签,如增加一个【BBS论坛】 2013-7-3011:18:51上传下载附件 (84.81KB) 增加页签   可以这么来做:进入BOSIDE,找到名称为主控台经典版,唯一标识为BOS_MainConsoleSutra的动态表单;为它写一个继承自AbstractDynamicFormPl......