安卓仿iphone日期选项器 仿iphone日期选择自定义控件
来源:互联网 发布:淘宝衣服拍摄 编辑:程序博客网 时间:2024/04/29 13:38
原来的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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint 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
0 0
- 安卓仿iphone日期选项器 仿iphone日期选择自定义控件
- 仿ios日期选择控件
- 仿Iphone样式的时间、日期选择器
- Android仿iPhone的日期时间选择器
- 自定义控件-仿iphone之ToggleButton&VoiceSeekBar
- 仿IOS7日期选择控件(新)
- Android 自定义日期控件 (仿QQ,IOS7)
- Android 自定义日期控件 (仿QQ,IOS7)
- 仿iphone日历控件
- iphone--日期简单方法
- iphone 激活日期查询
- iphone-NSDate日期处理
- Android 自定义滑动选择的日期控件
- 自定义Android日期时间选择控件DateTimePickerDialog
- 日期选择控件mobiscroll 自定义样式
- 自定义日期和时间选择组合控件
- iOS 自定义日期和数据源选择控件
- 仿ios时间,日期选择和三级联动控件
- 2015华为校园招聘 机试总结
- android如何写一个投票或是表达观点的界面
- 发酵乳,酸奶,酸乳
- Bestcoder round #3 1001的水题O(1)的查询
- SQL Server快捷键
- 安卓仿iphone日期选项器 仿iphone日期选择自定义控件
- Ubuntu 14.04.1 LTS上编译安装新的kernel
- A. Little Pony and Crystal Mine
- 习近平赢得人民信赖
- [leetcode]Reverse Nodes in k-Group 反转以k个节点为一组的链表
- 软件编程我该学习什么?
- 股份或幾乎覆蓋
- ios--视图的层次关系
- michael kors outlet xqhxrl yjm xjei