自定义时间选择器的实现

首先是布局文件time_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TableLayout
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TableRow>
         <NumberPicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/year_picker"/>
        <NumberPicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/month_picker"/>
        <NumberPicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/day_picker"/>
        </TableRow>
    </TableLayout>

    <TableLayout
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="horizontal">
        <TableRow>
        <NumberPicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/hour_picker"/>
        <NumberPicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/minute_picker"/>
        <NumberPicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/second_picker"/>
        </TableRow>
    </TableLayout>

</LinearLayout>



可见6个NumberPicker以2*3的形式排列。

接下来是自定义控件的实现

package com.example.TimeSelector;

import android.content.Context;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.NumberPicker;

/**
 * Created by Administrator on 2015/2/12.
 */
public class TimeSelector extends FrameLayout {
    NumberPicker npYear,npMonth,npDay,npHour,npMinute,npSecond;
    int year,month,day,hour,minute,second;
    public TimeSelector(Context context)
    {
        this(context, null);

    }
    public TimeSelector(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
    public TimeSelector(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        View v = LayoutInflater.from(context).inflate(R.layout.time_selector,this);
        npYear = (NumberPicker)v.findViewById(R.id.year_picker);
        npMonth = (NumberPicker)v.findViewById(R.id.month_picker);
        npDay = (NumberPicker)v.findViewById(R.id.day_picker);
        npHour = (NumberPicker)v.findViewById(R.id.hour_picker);
        npMinute = (NumberPicker)v.findViewById(R.id.minute_picker);
        npSecond = (NumberPicker)v.findViewById(R.id.second_picker);

        init();
    }

    public void init(){
        Time time = new Time("GMT+8");
        time.setToNow();
        year = time.year;
        month = time.month + 1;
        day = time.monthDay;
        hour = time.hour + 8;
        minute = time.minute;
        second = time.second;

        initBasicSettings(year,month,day,hour,minute,second);
        setTime();

        /**
         *change scale when date is changed
         */
        npYear.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                year  = i1;
                initBasicSettings(year,month,day,hour,minute,second);
                setTime();
            }
        });
        npMonth.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                month = i1;
                initBasicSettings(year,month,day,hour,minute,second);
                setTime();
            }
        });
        npDay.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                day = i1;
                setTime();
            }
        });
        npHour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                hour = i1;
                setTime();
            }
        });
        npMinute.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                minute = i1;
                setTime();
            }
        });
        npSecond.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                second = i1;
                setTime();
            }
        });
    } public void initBasicSettings(int year,int month,int day,int hour,int minute,int second){
        /**
         * basic settings
         */
        npYear.setMinValue(1970);
        npYear.setMaxValue(2100);
        npMonth.setMinValue(0);
        npMonth.setMaxValue(12);
        npDay.setMinValue(1);
        switch (month)
        {   //set days is 28 days or 29 when February
            case 1:
            case 3:
            case 5:
            case 7:
            case 10:
            case 12:
                npDay.setMaxValue(31);break;
            case 2:
                if (year%100 == 0){
                    if(year%400 == 0){
                        npDay.setMaxValue(29);
                    }
                }else{
                    if(year%4 == 0){
                        npDay.setMaxValue(29);
                    }
                    else {
                        npDay.setMaxValue(28);
                    }
                }
                break;
            default:
                npDay.setMaxValue(30);break;
        }
        npHour.setMinValue(0);
        npHour.setMaxValue(23);
        npMinute.setMinValue(0);
        npMinute.setMaxValue(59);
        npSecond.setMinValue(0);
        npSecond.setMaxValue(59);
    }
    public void setTime()
    {
        /**
         * set time to now
         */
        npYear.setValue(year);
        npMonth.setValue(month);
        npDay.setValue(day);
        npHour.setValue(hour);
        npMinute.setValue(minute);
        npSecond.setValue(second);
    }


}



其中主要涉及到NumberPicker控件的使用,注意setValue函数一定得在setMaxValue()和setMinValue()之后调用,不然数字都会显示为0。

如果想调用这个控件,在布局文件中声明即可,比如这里是main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <com.example.TimeSelector.TimeSelector
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:id = "@+id/ts"
            />

</LinearLayout>



最后在Activity中载入这个布局即可。

package com.example.TimeSelector;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

/**
 * Created by Administrator on 2015/2/12.
 */
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

    }
}



使用截图如下:

自定义时间选择器的实现

参考:

http://blog.csdn.net/sun_star1chen/article/details/16330459

源码下载:

https://github.com/bldhdh/TimeSelector

你可能感兴趣的:(自定义时间选择器的实现)