现在的位置: 首页 > 综合 > 正文

Android 自定义滑动选择的日期控件

2018年04月19日 ⁄ 综合 ⁄ 共 5630字 ⁄ 字号 评论关闭

由于Android系统自带的日期控件设计不是很友好,于是网上找了些资料,也自定义了一个日期控件。

效果图如下:


废话不说,直接上代码:

布局文件:wheel_calendar.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_marginTop="12dp"
    android:background="#FFFFFF"
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">
      
        <TextView
            android:id="@+id/txt_date"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:gravity="center"
            android:text="年"
            android:textColor="#000000"
            android:textSize="20dip"/>         
    </LinearLayout>
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">
      
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="年"
            android:textColor="#000000"
            android:textSize="20dip"/>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="月"
            android:textColor="#000000"
            android:textSize="20dip"/>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="日"
            android:textColor="#000000"
            android:textSize="20dip"/>         
    </LinearLayout>
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">
      
        <com.wheel.widget.WheelView android:id="@+id/year"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"/>
        <com.wheel.widget.WheelView android:id="@+id/month"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"/>
        <com.wheel.widget.WheelView android:id="@+id/day"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"/>         
    </LinearLayout>
</LinearLayout>

java类WheelCalendarActivity.java:

package com.example.demo;

import java.util.Calendar;

import com.wheel.widget.OnWheelChangedListener;
import com.wheel.widget.WheelView;
import com.wheel.widget.adapter.NumericWheelAdapter;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
 * @author star kong
 * */
public class WheelCalendarActivity extends BaseActivity{

	WheelView year,month,day; 
	int minYear = 1970;  //最小年份
	int fontSize = 20; 	 //字体大小
	TextView txt_date;
	
	int mYear=0,mMonth = 0,mDay = 0;
	String _week = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wheel_calendar);
		initView();
	}
	
	private void initView(){
		txt_date = (TextView)findViewById(R.id.txt_date);
        
        Calendar calendar = Calendar.getInstance();

        mYear =calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH)+1;
        mDay  = calendar.get(Calendar.DAY_OF_MONTH);
        getWeek(calendar.get(Calendar.DAY_OF_WEEK));
        
        month = (WheelView) findViewById(R.id.month);
        year = (WheelView) findViewById(R.id.year);
        day = (WheelView) findViewById(R.id.day);
        
        setShowDate();
        
        // month
        int curMonth = calendar.get(Calendar.MONTH);
        month.setViewAdapter(new DateNumericAdapter(this, 1, 12, curMonth));
        month.setCurrentItem(curMonth);
        month.addChangingListener(listener);
        month.setCyclic(true);
    
        // year
        int curYear = calendar.get(Calendar.YEAR);
        year.setViewAdapter(new DateNumericAdapter(this, minYear, curYear, curYear-minYear));
        year.setCurrentItem(curYear-minYear);
        year.addChangingListener(listener);
        year.setCyclic(true);
        
        //day
        day.setCyclic(true);
        updateDays(year, month, day);
        day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH)-1);
        day.addChangingListener(listener);
	}
	
	/**
     * Updates day wheel. Sets max days according to selected month and year
     */
    void updateDays(WheelView year, WheelView month, WheelView day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, minYear + year.getCurrentItem());
        calendar.set(Calendar.MONTH, month.getCurrentItem());
        
        int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        day.setViewAdapter(new DateNumericAdapter(this, 1, maxDays, calendar.get(Calendar.DAY_OF_MONTH) - 1));
        int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
        day.setCurrentItem(curDay - 1, true);
        
        mYear = minYear+year.getCurrentItem();
        mMonth = month.getCurrentItem()+1;
        mDay = curDay;
        calendar.set(Calendar.DAY_OF_MONTH, mDay);
        getWeek(calendar.get(Calendar.DAY_OF_WEEK));
        
    }
    
    private void setShowDate(){
        txt_date.setText(mYear+"-"+mMonth+"-"+mDay+"   星期"+_week);
    }
    
    /**
     * 获取星期几
     * */
    private void getWeek(int week){
    	switch(week){
    	case 1:
    		_week = "天";
    		break;
    	case 2:
    		_week = "一";
    		break;
    	case 3:
    		_week = "二";
    		break;
    	case 4:
    		_week = "三";
    		break;
    	case 5:
    		_week = "四";
    		break;
    	case 6:
    		_week = "五";
    		break;
    	case 7:
    		_week = "六";
    		break;
    	}
    }
    
    OnWheelChangedListener listener = new OnWheelChangedListener() {
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            updateDays(year, month, day);
            setShowDate();
        }
    };
    
    /**
     * Adapter for numeric wheels. Highlights the current value.
     */
    private class DateNumericAdapter extends NumericWheelAdapter {
        // Index of current item
        int currentItem;
        // Index of item to be highlighted
        int currentValue;
        
        /**
         * Constructor
         */
        public DateNumericAdapter(Context context, int minValue, int maxValue, int current) {
            super(context, minValue, maxValue);
            this.currentValue = current;
            setTextSize(fontSize);
        }
        
        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);
            if (currentItem == currentValue) {
                view.setTextColor(0xFF0000F0);
            }
            view.setTypeface(Typeface.SANS_SERIF);
        }
        
        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }
	
}

类中有使用第三方代码,均为外国高人编写,由于代码较多,我已导出为jar文件在我资源中可以下载。

http://download.csdn.net/detail/kongxingxing/5886089

点击打开链接


抱歉!评论已关闭.