Android开发之时间选择器

有时我们开发一款 App 有这样的需求:
1,自定义年月日;
2,自定义年月日时分。

先看效果图:

1,首先在 app 文件夹 build.gradle 文件中添加:

compile 'com.jzxiang.pickerview:TimePickerDialog:1.0.0'

2,自定义参数:

long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;   //L 就如float类型  0.5f 一个意思。
TimePickerDialog mDialogAll = new TimePickerDialog.Builder()
        .setCallBack(this)  //回调
        .setCancelStringId("Cancel")//取消按钮
        .setSureStringId("Sure")//确定按钮
        .setTitleStringId("TimePicker")//标题
        .setYearText("Year")//Year
        .setMonthText("Month")//Month
        .setDayText("Day")//Day
        .setHourText("Hour")//Hour
        .setMinuteText("Minute")//Minute
        .setCyclic(false)//是否可循环
        .setMinMillseconds(System.currentTimeMillis())//最小日期和时间
        .setMaxMillseconds(System.currentTimeMillis() + tenYears)//最大日期和时间
        .setCurrentMillseconds(System.currentTimeMillis())
        .setThemeColor(getResources().getColor(R.color.timepicker_dialog_bg))
        .setType(Type.ALL)//类型
     .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))//未选中的文本颜色
        .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))//当前文本颜色
        .setWheelItemTextSize(12)//字体大小
        .build();

//这一步很重要,不然不会显示。
mDialogAll.show(getSupportFragmentManager(), "time");

注意:如果想去掉确定或取消按钮,可以这样:

setCancelStringId(null)
setCancelStringId("")

or
setSureStringId("")
setSureStringId(null)

3,activity_main.xml 布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color1"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/color1"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:background="@null"
            android:gravity="center"
            android:text="@string/select_date"
            android:textColor="@color/color3"
            android:textSize="15sp" />

    LinearLayout>

    <RelativeLayout
        android:id="@+id/selectDate"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/color2"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:background="@null"
            android:gravity="center"
            android:text="@string/current_date"
            android:textColor="@color/color4"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/currentDate"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:background="@null"
            android:gravity="center"
            android:paddingEnd="15dp"
            android:paddingStart="15dp"
            android:textColor="@color/color5"
            android:textSize="15sp" />

    RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/color1"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:background="@null"
            android:gravity="center"
            android:text="@string/select_time"
            android:textColor="@color/color3"
            android:textSize="15sp" />

    LinearLayout>

    <RelativeLayout
        android:id="@+id/selectTime"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/color2"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:background="@null"
            android:gravity="center"
            android:text="@string/current_time"
            android:textColor="@color/color4"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/currentTime"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:background="@null"
            android:gravity="center"
            android:paddingEnd="15dp"
            android:paddingStart="15dp"
            android:textColor="@color/color5"
            android:textSize="15sp" />

    RelativeLayout>


LinearLayout>

4,在代码中愉快的使用:

package com.gyq.customdatepicker;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnDateSetListener {
    private RelativeLayout selectDate, selectTime;
    private TextView currentDate, currentTime;

    private TimePickerDialog mDialogAll,mDialogYearMonthDay;

    long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;

    SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd HH:mm");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        selectTime = (RelativeLayout) findViewById(R.id.selectTime);
        selectTime.setOnClickListener(this);
        selectDate = (RelativeLayout) findViewById(R.id.selectDate);
        selectDate.setOnClickListener(this);
        currentDate = (TextView) findViewById(R.id.currentDate);
        currentTime = (TextView) findViewById(R.id.currentTime);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.selectDate:
                mDialogYearMonthDay = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH_DAY)
                        .setCallBack(new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                                String text = getDateToString(millseconds);
                                currentDate.setText(text);
                            }
                        })
                        .setCancelStringId("取消")
                        .setSureStringId("确定")
                        .setTitleStringId("自定义提醒日期")
                        .setYearText("年")
                        .setMonthText("月")
                        .setDayText("日")
                        .setCyclic(false)
                        .setMinMillseconds(System.currentTimeMillis())
                        .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                        .setCurrentMillseconds(System.currentTimeMillis())
                        .setThemeColor(getResources().getColor(R.color.dialog_bg))
                        .setWheelItemTextNormalColor(getResources().getColor(R.color.dialog_text))
                        .setWheelItemTextSelectorColor(getResources().getColor(R.color.dialog_select))
                        .setWheelItemTextSize(12)
                        .build();

                mDialogYearMonthDay.show(getSupportFragmentManager(),"date");
                break;

            case R.id.selectTime:
                mDialogAll = new TimePickerDialog.Builder()
                        .setCallBack(this)
                        .setCancelStringId("取消")
                        .setSureStringId("确定")
                        .setTitleStringId("自定义提醒时间")
                        .setYearText("年")
                        .setMonthText("月")
                        .setDayText("日")
                        .setHourText("时")
                        .setMinuteText("分")
                        .setCyclic(false)
                        .setMinMillseconds(System.currentTimeMillis())
                        .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                        .setCurrentMillseconds(System.currentTimeMillis())
                        .setThemeColor(getResources().getColor(R.color.dialog_bg))
                        .setType(Type.ALL)
                        .setWheelItemTextNormalColor(getResources().getColor(R.color.dialog_text))
                        .setWheelItemTextSelectorColor(getResources().getColor(R.color.dialog_select))
                        .setWheelItemTextSize(12)
                        .build();

                mDialogAll.show(getSupportFragmentManager(), "time");
                break;
        }
    }


    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        String text = getDateToString(millseconds);
        currentTime.setText(text);
    }

    public String getDateToString(long time) {
        Date d = new Date(time);
        return sf.format(d);
    }
}

附上源码:TimePickerDialog

你可能感兴趣的:(Android开源框架)