自定义日期选择控件(适用于其他选择功能)

Demo自定义了一个时间选择控件,可以精确到秒,同时选择器里的文字可以自己设置,适用于其他的选择功能,下面是截图和代码:



首先是选择器布局文件的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <NumberPicker
            android:id="@+id/numberPicker1"
            android:layout_width="50dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年"
            android:textColor="#000000" />

        <NumberPicker
            android:id="@+id/numberPicker2"
            android:layout_width="50dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="月"
            android:textColor="#000000" />

        <NumberPicker
            android:id="@+id/numberPicker3"
            android:layout_width="50dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="日"
            android:textColor="#000000" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center" >

        <NumberPicker
            android:id="@+id/numberPicker4"
            android:layout_width="50dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="时"
            android:textColor="#000000" />

        <NumberPicker
            android:id="@+id/numberPicker5"
            android:layout_width="50dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="分"
            android:textColor="#000000" />

        <NumberPicker
            android:id="@+id/numberPicker6"
            android:layout_width="50dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="秒"
            android:textColor="#000000" />
    </LinearLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确定" />

</LinearLayout>

接下来是自定义控件MyNumberPicker的定义:

package com.example.datetime;

import java.lang.reflect.Field;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.NumberPicker;

public class MyNumberPicker extends LinearLayout {

	NumberPicker yearP;
	NumberPicker monthP;
	NumberPicker dateP;

	public MyNumberPicker(Context context) {
		this(context, null);
	}

	public MyNumberPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		removeAllViews();
		initView(context);
	}

	private void initView(Context context) {

		yearP = new NumberPicker(context);
		monthP = new NumberPicker(context);
		dateP = new NumberPicker(context);

		setNumberPickerTextColor(yearP, Color.BLACK);
		setNumberPickerTextColor(monthP, Color.BLACK);
		setNumberPickerTextColor(dateP, Color.BLACK);

		Date date = new Date(System.currentTimeMillis());
		yearP.setMaxValue(23);
		yearP.setMinValue(0);
		yearP.setDisplayedValues(new String[] { "00", "01", "02", "03", "04",
				"05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
				"15", "16", "17", "18", "19", "20", "21", "22", "23" });
		monthP.setMaxValue(59);
		monthP.setMinValue(0);
		monthP.setDisplayedValues(new String[] { "00", "01", "02", "03", "04",
				"05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
				"15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
				"25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
				"35", "36", "37", "38", "39", "40", "41", "42", "43", "44",
				"45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
				"55", "56", "57", "58", "59" });
		dateP.setMaxValue(59);
		dateP.setMinValue(0);
		dateP.setDisplayedValues(new String[] { "00", "01", "02", "03", "04",
				"05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
				"15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
				"25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
				"35", "36", "37", "38", "39", "40", "41", "42", "43", "44",
				"45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
				"55", "56", "57", "58", "59" });
		// yearP.setMaxValue(date.getYear() + 2000);
		// yearP.setMinValue(date.getYear() + 1900);
		// monthP.setMaxValue(12);
		// monthP.setMinValue(1);
		// monthP.setDisplayedValues(new String[] { "01", "02", "03", "04",
		// "05",
		// "06", "07", "08", "09", "10", "11", "12" });
		// dateP.setMaxValue(31);
		// dateP.setMinValue(1);
		// dateP.setDisplayedValues(new String[] { "01", "02", "03", "04", "05",
		// "06", "07", "08", "09", "10", "11", "12", "13", "14", "15",
		// "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
		// "26", "27", "28", "29", "30", "31" });
		//
		// monthP.setValue(date.getMonth() + 1);
		// dateP.setValue(date.getDay());
		yearP.setValue(date.getHours());
		monthP.setValue(date.getMinutes());
		dateP.setValue(date.getSeconds());
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);

		params.weight = 100;

		addView(yearP, params);
		addView(monthP, params);
		addView(dateP, params);

	}

	public static boolean setNumberPickerTextColor(NumberPicker numberPicker,
			int color) {
		boolean result = false;
		final int count = numberPicker.getChildCount();
		for (int i = 0; i < count; i++) {
			View child = numberPicker.getChildAt(i);
			if (child instanceof EditText) {
				try {
					Field selectorWheelPaintField = numberPicker.getClass()
							.getDeclaredField("mSelectorWheelPaint");
					selectorWheelPaintField.setAccessible(true);
					((Paint) selectorWheelPaintField.get(numberPicker))
							.setColor(color);
					((EditText) child).setTextColor(color);
					numberPicker.invalidate();
					result = true;
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}

MainActivity的代码:

package com.example.datetime;

import java.lang.reflect.Field;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	NumberPicker nu1;
	NumberPicker nu2;
	NumberPicker nu3;
	NumberPicker nu4;
	NumberPicker nu5;
	NumberPicker nu6;
	Dialog dialog;
	TextView textView;
	Button button;
	Button button2;

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

		button = (Button) findViewById(R.id.button1);
		textView = (TextView) findViewById(R.id.textView1);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDateSelectDialog();
			}
		});

	}

	public void showDateSelectDialog() {

		Builder builder = new Builder(MainActivity.this);

		View view = LayoutInflater.from(getApplicationContext()).inflate(
				R.layout.timepicker_dialog, null);

		nu1 = (NumberPicker) view.findViewById(R.id.numberPicker1);
		nu2 = (NumberPicker) view.findViewById(R.id.numberPicker2);
		nu3 = (NumberPicker) view.findViewById(R.id.numberPicker3);
		nu4 = (NumberPicker) view.findViewById(R.id.numberPicker4);
		nu5 = (NumberPicker) view.findViewById(R.id.numberPicker5);
		nu6 = (NumberPicker) view.findViewById(R.id.numberPicker6);
		button2 = (Button) view.findViewById(R.id.button2);

		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				String time = nu1.getValue() + "年" + nu2.getValue() + "月"
						+ nu3.getValue() + "日" + nu4.getValue() + "时"
						+ nu5.getValue() + "分" + nu6.getValue() + "秒";
				textView.setText(time);
				dialog.cancel();
			}
		});
		setNumberPickerTextColor(nu1, Color.BLUE);
		setNumberPickerTextColor(nu2, Color.BLUE);
		setNumberPickerTextColor(nu3, Color.BLUE);
		setNumberPickerTextColor(nu4, Color.BLUE);
		setNumberPickerTextColor(nu5, Color.BLUE);
		setNumberPickerTextColor(nu6, Color.BLUE);

		nu1.setMaxValue(2100);
		nu1.setMinValue(1900);
		// nu1.setDisplayedValues(new String[]{"第一","第二","第三","第四"});
		nu2.setMaxValue(12);
		nu2.setMinValue(1);
		// nu1.setDisplayedValues(new String[]{"第一","第二","第三","第四"});
		nu3.setMaxValue(31);
		nu3.setMinValue(1);
		// nu1.setDisplayedValues(new String[]{"第一","第二","第三","第四"});
		nu4.setMaxValue(23);
		nu4.setMinValue(0);
		// nu1.setDisplayedValues(new String[]{"第一","第二","第三","第四"});
		nu5.setMaxValue(60);
		nu5.setMinValue(0);
		// nu1.setDisplayedValues(new String[]{"第一","第二","第三","第四"});
		nu6.setMaxValue(60);
		nu6.setMinValue(0);
		// nu6.setDisplayedValues(new String[] { "01", "02", "03", "04" });
		builder.setView(view);

		builder.setTitle("时间选择可以精确到秒");
		dialog = builder.create();
		dialog.show();

		nu6.setOnValueChangedListener(new OnValueChangeListener() {

			@Override
			public void onValueChange(NumberPicker picker, int oldVal,
					int newVal) {
				// TODO Auto-generated method stub
				System.out.println("数据的改变" + oldVal + "ddd:" + newVal);
			}
		});
	}

	public static boolean setNumberPickerTextColor(NumberPicker numberPicker,
			int color) {
		boolean result = false;
		final int count = numberPicker.getChildCount();
		for (int i = 0; i < count; i++) {
			View child = numberPicker.getChildAt(i);
			if (child instanceof EditText) {
				try {
					Field selectorWheelPaintField = numberPicker.getClass()
							.getDeclaredField("mSelectorWheelPaint");
					selectorWheelPaintField.setAccessible(true);
					((Paint) selectorWheelPaintField.get(numberPicker))
							.setColor(color);
					((EditText) child).setTextColor(color);
					numberPicker.invalidate();
					result = true;
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}

主界面first.xml文件:

<?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="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击选择时间" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00-00-00" />

</LinearLayout>

源码下载地址

http://download.csdn.net/detail/jl_stone/9667232

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值