安卓仿iphone日期选项器 仿iphone日期选择自定义控件

原来的DatePicker总有这样那样的不如意,看到ipad的日期选择,感觉很好,如果google android date like iphone,

得到Android Wheel,项目地址:https://code.google.com/p/android-wheel/

现在国内被墙了,下载的话,还比较麻烦,总之费劲down下来,并研究实现。

1.首先,导入down下来的wheel,导入后得到wheel工程

2.新建一个新的安卓工程DateWheel,默认配制,完成后,在工程上右键,properties,android,把wheel加入Lib



3.建立layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="请选择日期"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/layout_bg"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:orientation="horizontal" >

        <kankan.wheel.widget.WheelView
            android:id="@+id/id_year"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </kankan.wheel.widget.WheelView>

        <kankan.wheel.widget.WheelView
            android:id="@+id/id_month"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </kankan.wheel.widget.WheelView>

        <kankan.wheel.widget.WheelView
            android:id="@+id/id_day"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </kankan.wheel.widget.WheelView>
    </LinearLayout>
    
    <Button 
        android:id="@+id/id_button"
		android:layout_gravity="right"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="确定"
        />
    

</LinearLayout>

4.在Activity增加代码:

<pre name="code" class="java">package com.example.datewheel;

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


import java.util.Calendar;
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import kankan.wheel.widget.adapters.NumericWheelAdapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DateActivity extends Activity {
	private Button button;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day);

        Calendar calendar = Calendar.getInstance();
        int curYear,curMonth,curDay;
        final WheelView month = (WheelView) findViewById(R.id.id_month);
        final WheelView year = (WheelView) findViewById(R.id.id_year);
        final WheelView day = (WheelView) findViewById(R.id.id_day);
        
        OnWheelChangedListener listener = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                updateDays(year, month, day);
            }
        };

	    // month,beginning from 0
	    curMonth = calendar.get(Calendar.MONTH);
	    String months[] = new String[] {this.getString(R.string.january), this.getString(R.string.february),
	    		this.getString(R.string.march), this.getString(R.string.april), this.getString(R.string.may), 
	    		this.getString(R.string.june), this.getString(R.string.july), this.getString(R.string.august), 
	    		this.getString(R.string.september), this.getString(R.string.october), 
	    		this.getString(R.string.november),this.getString(R.string.december)};
	    month.setViewAdapter(new DateArrayAdapter(this, months, curMonth));
	    //设置当前月份
	    month.setCurrentItem(curMonth);
	    month.addChangingListener(listener);
	
	    //得到当前年
	    curYear = calendar.get(Calendar.YEAR);
	    year.setViewAdapter(new DateNumericAdapter(this, curYear, curYear + 50, 0));
	    //设置当前年
	    year.setCurrentItem(curYear);
	    year.addChangingListener(listener);
	    
	    //day
	    updateDays(year, month, day);
	    //设置当前天 从0开始
	    curDay=calendar.get(Calendar.DAY_OF_MONTH);
	    day.setCurrentItem(curDay);
	    
	    //botton listener
	    button=(Button) findViewById(R.id.id_button);
	    button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int selecty,selectm,selectd;
				selecty=year.getCurrentItem();
				selectm=month.getCurrentItem();
				selectd=day.getCurrentItem();
				String strtmp=String.format("选择是:%d-%02d-%02d", curYear+selecty,selectm+1,selectd+1);
				Toast.makeText(DateActivity.this, strtmp, 1).show(); 
			}
		});
    }
    
    /**
     * 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, calendar.get(Calendar.YEAR) + 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 maxDay = Math.min(maxDays, day.getCurrentItem() + 1);
        day.setCurrentItem(maxDay - 1, true);
    }
    
    /**
     * 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(16);
        }
        
        @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);
        }
    }
    
    /**
     * Adapter for string based wheel. Highlights the current value.
     */
    private class DateArrayAdapter extends ArrayWheelAdapter<String> {
        // Index of current item
        int currentItem;
        // Index of item to be highlighted
        int currentValue;
        
        /**
         * Constructor
         */
        public DateArrayAdapter(Context context, String[] items, int current) {
            super(context, items);
            this.currentValue = current;
            setTextSize(16);
        }
        
        @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);
        }
    }

}


 5效果图 



6源码下载

http://download.csdn.net/detail/mindlead/7718191


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值