实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.byl.datepicker;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.byl.datepicker.wheelview.OnWheelScrollListener;
import com.byl.datepicker.wheelview.WheelView;
import com.byl.datepicker.wheelview.adapter.ArrayWheelAdapter;
import com.byl.datepicker.wheelview.adapter.NumericWheelAdapter;
import com.byl.datepicker.wheelview.adapter.WeekWheelAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private LayoutInflater inflater = null;
private WheelView year;
private WheelView month;
private WheelView day;
private WheelView time;
private WheelView min;
private WheelView sec;
private WheelView week;
private int mYear=1996;
private int mMonth=0;
private int mDay=1;
LinearLayout ll;
TextView tv1,tv2;
View view=null;
boolean isMonthSetted=false,isDaySetted=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
ll=(LinearLayout) findViewById(R.id.ll);
ll.addView(getDataPick());
tv1=(TextView) findViewById(R.id.tv1);//年龄
tv2=(TextView) findViewById(R.id.tv2);//星座
}
private View getDataPick() {
Calendar c = Calendar.getInstance();
int norYear = c.get(Calendar.YEAR);
// int curMonth = c.get(Calendar.MONTH) 1;//通过Calendar算出的月数要 1
// int curDate = c.get(Calendar.DATE);
int curYear = mYear;
int curMonth =mMonth 1;
int curDate = mDay;
view = inflater.inflate(R.layout.wheel_date_picker, null);
year = (WheelView) view.findViewById(R.id.year);
NumericWheelAdapter numericWheelAdapter1=new NumericWheelAdapter(this,1950, norYear);
numericWheelAdapter1.setLabel("年");
year.setViewAdapter(numericWheelAdapter1);
year.setCyclic(true);//是否可循环滑动
year.addScrollingListener(scrollListener);
month = (WheelView) view.findViewById(R.id.month);
NumericWheelAdapter numericWheelAdapter2=new NumericWheelAdapter(this,1, 12, "%02d");
numericWheelAdapter2.setLabel("月");
month.setViewAdapter(numericWheelAdapter2);
month.setCyclic(true);
month.addScrollingListener(scrollListener);
day = (WheelView) view.findViewById(R.id.day);
initDay(curYear,curMonth);
day.setCyclic(true);
min = (WheelView) view.findViewById(R.id.min);
NumericWheelAdapter numericWheelAdapter3=new NumericWheelAdapter(this,1, 23, "%02d");
numericWheelAdapter3.setLabel("时");
min.setViewAdapter(numericWheelAdapter3);
min.setCyclic(true);
min.addScrollingListener(scrollListener);
sec = (WheelView) view.findViewById(R.id.sec);
NumericWheelAdapter numericWheelAdapter4=new NumericWheelAdapter(this,1, 59, "%02d");
numericWheelAdapter4.setLabel("分");
sec.setViewAdapter(numericWheelAdapter4);
sec.setCyclic(true);
sec.addScrollingListener(scrollListener);
week= (WheelView) view.findViewById(R.id.week);
WeekWheelAdapter weekWheelAdapter5=new WeekWheelAdapter(this,1, 6, "%02d");
weekWheelAdapter5.setLabel("星期");
week.setViewAdapter(weekWheelAdapter5);
week.setCyclic(true);
week.addScrollingListener(scrollListener);
year.setVisibleItems(7);//设置显示行数
month.setVisibleItems(7);
day.setVisibleItems(7);
min.setVisibleItems(7);
sec.setVisibleItems(7);
week.setVisibleItems(7);
year.setCurrentItem(curYear - 1950);
month.setCurrentItem(curMonth - 1);
day.setCurrentItem(curDate - 1);
return view;
}
OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
@Override
public void onScrollingStarted(WheelView wheel) {
}
@Override
public void onScrollingFinished(WheelView wheel) {
int n_year = year.getCurrentItem() 1950;//年
int n_month = month.getCurrentItem() 1;//月
initDay(n_year,n_month);
String birthday=new StringBuilder().append((year.getCurrentItem() 1950)).append("-").append((month.getCurrentItem() 1) < 10 ? "0" (month.getCurrentItem() 1) : (month.getCurrentItem() 1)).append("-").append(((day.getCurrentItem() 1) < 10) ? "0" (day.getCurrentItem() 1) : (day.getCurrentItem() 1)).toString();
tv1.setText("年龄 " calculateDatePoor(birthday) "岁");
tv2.setText("星座 " getAstro(month.getCurrentItem() 1,day.getCurrentItem() 1));
Toast.makeText(MainActivity.this, week.getCurrentItem() "",
Toast.LENGTH_LONG).show();
}
};
/**
*
* @param year
* @param month
* @return
*/
private int getDay(int year, int month) {
int day = 30;
boolean flag = false;
switch (year % 4) {
case 0:
flag = true;
break;
default:
flag = false;
break;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
day = flag ? 29 : 28;
break;
default:
day = 30;
break;
}
return day;
}
/**
*/
private void initDay(int arg1, int arg2) {
NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(this,1, getDay(arg1, arg2), "%02d");
numericWheelAdapter.setLabel("日");
day.setViewAdapter(numericWheelAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 根据日期计算年龄
* @param birthday
* @return
*/
public static final String calculateDatePoor(String birthday) {
try {
if (TextUtils.isEmpty(birthday))
return "0";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birthdayDate = sdf.parse(birthday);
String currTimeStr = sdf.format(new Date());
Date currDate = sdf.parse(currTimeStr);
if (birthdayDate.getTime() > currDate.getTime()) {
return "0";
}
long age = (currDate.getTime() - birthdayDate.getTime())
/ (24 * 60 * 60 * 1000) 1;
String year = new DecimalFormat("0.00").format(age / 365f);
if (TextUtils.isEmpty(year))
return "0";
return String.valueOf(new Double(year).intValue());
} catch (ParseException e) {
e.printStackTrace();
}
return "0";
}
/**
* 根据月日计算星座
* @param month
* @param day
* @return
*/
public String getAstro(int month, int day) {
String[] astro = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座",
"双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座" };
int[] arr = new int[] { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };// 两个星座分割日
int index = month;
// 所查询日期在分割日之前,索引-1,否则不变
if (day < arr[month - 1]) {
index = index - 1;
}
// 返回索引指向的星座string
return astro[index];
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论