赞
踩
今天公司让做一个滚动选择日期的功能,在网上搜到了很多,但一放到自己的项目中就变成了按钮点击的效果,后来发现只要设置Theme滚动效果就会消失。
滚动控件参考:
http://www.cnblogs.com/tiantianbyconan/p/3819304.html
https://github.com/wangjiegulu/WheelView
源代码中还需要引入两个依赖工程,这里通过提取依赖工程的代码对其进行了简化。
1、滚动控件WheelView代码:
- package com.cx.datechoosedialog;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.ColorFilter;
- import android.graphics.Paint;
- import android.graphics.drawable.Drawable;
- import android.util.AttributeSet;
- import android.util.TypedValue;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- import android.widget.ScrollView;
- import android.widget.TextView;
-
- /**
- * Author: wangjie
- * Email: tiantian.china.2@gmail.com
- * Date: 7/1/14.
- */
- public class WheelView extends ScrollView {
- public static final String TAG = WheelView.class.getSimpleName();
-
- public static class OnWheelViewListener {
- public void onSelected(int selectedIndex, String item) {
- }
-
- ;
- }
-
-
- private Context context;
- // private ScrollView scrollView;
-
- private LinearLayout views;
-
- public WheelView(Context context) {
- super(context);
- init(context);
- }
-
- public WheelView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(context);
- }
-
- public WheelView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init(context);
- }
-
- // String[] items;
- List<String> items;
-
- private List<String> getItems() {
- return items;
- }
-
- public void setItems(List<String> list) {
- // if (null == items) {
- items = new ArrayList<String>();
- // }
- items.clear();
- items.addAll(list);
-
- // 前面和后面补全
- for (int i = 0; i < offset; i++) {
- items.add(0, "");
- items.add("");
- }
-
- initData();
-
- }
-
-
- public static final int OFF_SET_DEFAULT = 1;
- int offset = OFF_SET_DEFAULT; // 偏移量(需要在最前面和最后面补全)
-
- public int getOffset() {
- return offset;
- }
-
- public void setOffset(int offset) {
- this.offset = offset;
- }
-
- int displayItemCount; // 每页显示的数量
-
- int selectedIndex = 1;
-
-
- private void init(Context context) {
- this.context = context;
-
- this.setVerticalScrollBarEnabled(false);
-
- views = new LinearLayout(context);
- views.setOrientation(LinearLayout.VERTICAL);
- this.addView(views);
-
- scrollerTask = new Runnable() {
-
- public void run() {
-
- int newY = getScrollY();
- if (initialY - newY == 0) { // stopped
- final int remainder = initialY % itemHeight;
- final int divided = initialY / itemHeight;
- if (remainder == 0) {
- selectedIndex = divided + offset;
-
- onSeletedCallBack();
- } else {
- if (remainder > itemHeight / 2) {
- WheelView.this.post(new Runnable() {
- @Override
- public void run() {
- WheelView.this.smoothScrollTo(0, initialY - remainder + itemHeight);
- selectedIndex = divided + offset + 1;
- onSeletedCallBack();
- }
- });
- } else {
- WheelView.this.post(new Runnable() {
- @Override
- public void run() {
- WheelView.this.smoothScrollTo(0, initialY - remainder);
- selectedIndex = divided + offset;
- onSeletedCallBack();
- }
- });
- }
-
-
- }
-
-
- } else {
- initialY = getScrollY();
- WheelView.this.postDelayed(scrollerTask, newCheck);
- }
- }
- };
-
-
- }
-
- int initialY;
-
- Runnable scrollerTask;
- int newCheck = 50;
-
- public void startScrollerTask() {
-
- initialY = getScrollY();
- this.postDelayed(scrollerTask, newCheck);
- }
-
- private void initData() {
- views.removeAllViews();
-
- displayItemCount = offset * 2 + 1;
-
- for (String item : items) {
- views.addView(createView(item));
- }
-
- refreshItemView(0);
- }
-
- int itemHeight = 0;
-
- private TextView createView(String item) {
- TextView tv = new TextView(context);
- tv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
- tv.setSingleLine(true);
- tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
- tv.setText(item);
- tv.setGravity(Gravity.CENTER);
- int padding = Utility.dip2px(context, 15);
- tv.setPadding(padding, padding, padding, padding);
- if (0 == itemHeight) {
- itemHeight = Utility.getViewMeasuredHeight(tv);
- views.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight * displayItemCount));
- LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) this.getLayoutParams();
- this.setLayoutParams(new LinearLayout.LayoutParams(lp.width, itemHeight * displayItemCount));
- }
- return tv;
- }
-
- @Override
- protected void onScrollChanged(int l, int t, int oldl, int oldt) {
- super.onScrollChanged(l, t, oldl, oldt);
-
- refreshItemView(t);
-
- if (t > oldt) {
- // Logger.d(TAG, "向下滚动");
- scrollDirection = SCROLL_DIRECTION_DOWN;
- } else {
- // Logger.d(TAG, "向上滚动");
- scrollDirection = SCROLL_DIRECTION_UP;
-
- }
-
-
- }
-
- private void refreshItemView(int y) {
- int position = y / itemHeight + offset;
- int remainder = y % itemHeight;
- int divided = y / itemHeight;
-
- if (remainder == 0) {
- position = divided + offset;
- } else {
- if (remainder > itemHeight / 2) {
- position = divided + offset + 1;
- }
- }
-
- int childSize = views.getChildCount();
- for (int i = 0; i < childSize; i++) {
- TextView itemView = (TextView) views.getChildAt(i);
- if (null == itemView) {
- return;
- }
- if (position == i) {
- itemView.setTextColor(Color.parseColor("#0288ce"));
- } else {
- itemView.setTextColor(Color.parseColor("#bbbbbb"));
- }
- }
- }
-
- /**
- * 获取选中区域的边界
- */
- int[] selectedAreaBorder;
-
- private int[] obtainSelectedAreaBorder() {
- if (null == selectedAreaBorder) {
- selectedAreaBorder = new int[2];
- selectedAreaBorder[0] = itemHeight * offset;
- selectedAreaBorder[1] = itemHeight * (offset + 1);
- }
- return selectedAreaBorder;
- }
-
-
- private int scrollDirection = -1;
- private static final int SCROLL_DIRECTION_UP = 0;
- private static final int SCROLL_DIRECTION_DOWN = 1;
-
- Paint paint;
- int viewWidth;
-
- @Override
- public void setBackgroundDrawable(Drawable background) {
-
- if (viewWidth == 0) {
- viewWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
- }
-
- if (null == paint) {
- paint = new Paint();
- paint.setColor(Color.parseColor("#83cde6"));
- paint.setStrokeWidth(Utility.dip2px(context, 1f));
- }
-
- background = new Drawable() {
- @Override
- public void draw(Canvas canvas) {
- canvas.drawLine(viewWidth * 1 / 6, obtainSelectedAreaBorder()[0], viewWidth * 5 / 6, obtainSelectedAreaBorder()[0], paint);
- canvas.drawLine(viewWidth * 1 / 6, obtainSelectedAreaBorder()[1], viewWidth * 5 / 6, obtainSelectedAreaBorder()[1], paint);
- }
-
- @Override
- public void setAlpha(int alpha) {
-
- }
-
- @Override
- public void setColorFilter(ColorFilter cf) {
-
- }
-
- @Override
- public int getOpacity() {
- return 0;
- }
- };
-
-
- super.setBackgroundDrawable(background);
-
- }
-
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- viewWidth = w;
- setBackgroundDrawable(null);
- }
-
- /**
- * 选中回调
- */
- private void onSeletedCallBack() {
- if (null != onWheelViewListener) {
- onWheelViewListener.onSelected(selectedIndex, items.get(selectedIndex));
- }
-
- }
-
- public void setSeletion(int position) {
- final int p = position;
- selectedIndex = p + offset;
- this.post(new Runnable() {
- @Override
- public void run() {
- WheelView.this.smoothScrollTo(0, p * itemHeight);
- }
- });
-
- }
-
- public String getSeletedItem() {
- return items.get(selectedIndex);
- }
-
- public int getSeletedIndex() {
- return selectedIndex - offset;
- }
-
-
- @Override
- public void fling(int velocityY) {
- super.fling(velocityY / 3);
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- if (ev.getAction() == MotionEvent.ACTION_UP) {
-
- startScrollerTask();
- }
- return super.onTouchEvent(ev);
- }
-
- private OnWheelViewListener onWheelViewListener;
-
- public OnWheelViewListener getOnWheelViewListener() {
- return onWheelViewListener;
- }
-
- public void setOnWheelViewListener(OnWheelViewListener onWheelViewListener) {
- this.onWheelViewListener = onWheelViewListener;
- }
-
-
- }
-
2、依赖工程中提取的方法
- package com.cx.datechoosedialog;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
-
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.view.View;
-
- @SuppressLint("SimpleDateFormat")
- public class Utility {
-
- /**
- * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
- */
- public static int dip2px(Context context, float dpValue) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dpValue * scale + 0.5f);
- }
-
- /**
- * 获取控件的高度,如果获取的高度为0,则重新计算尺寸后再返回高度
- *
- * @param view
- * @return
- */
- public static int getViewMeasuredHeight(View view) {
- calcViewMeasure(view);
- return view.getMeasuredHeight();
- }
-
- /**
- * 测量控件的尺寸
- *
- * @param view
- */
- public static void calcViewMeasure(View view) {
- int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
- int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
- view.measure(width, expandSpec);
- }
-
- public static String dateFormat(Date date) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
- return sdf.format(date);
- }
-
- public static String dateFormatDay(Date date) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(date);
- }
-
- /****
- * 获取月末最后一天
- *
- * @param sDate
- * 2014-11-24
- * @return 30
- */
- public static String getMonthMaxDay(String sDate) {
- SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal = Calendar.getInstance();
- Date date = null;
- try {
- date = sdf_full.parse(sDate + "-01");
- } catch (ParseException e) {
- e.printStackTrace();
- }
- cal.setTime(date);
- int last = cal.getActualMaximum(Calendar.DATE);
- return String.valueOf(last);
- }
- }
通过自定义Dialog实现日期选择控件,这里也可以控制日期的可选范围
- package com.cx.datechoosedialog;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- import android.annotation.SuppressLint;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.TextView;
-
- import com.cx.datechoosedialog.WheelView.OnWheelViewListener;
-
- public class DateChooseDialog extends Dialog {
-
- public DateChooseDialog(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
-
- public DateChooseDialog(Context context, int theme) {
- super(context, theme);
- }
-
- @SuppressLint("NewApi")
- public static class Builder{
- private Context context;
- private String title;
- private int yearsSeletion;
- private int monthSeletion;
- private int daySeletion;
- private List<String> yearsItems;
- private List<String> monthItems;
- private List<String> dayItems;
- private TextView textView;
- private TextView tvTime;
- private WheelView wheelView1, wheelView2, wheelView3;
- private Button positiveButton, negativeButton;
- private DialogInterface.OnClickListener positiveButtonClickListener;
- private DialogInterface.OnClickListener negativeButtonClickListener;
- private String years;
- private String month;
- private String day;
-
- public Builder(Context context) {
- this.context = context;
- yearsItems = new ArrayList<String>();
- monthItems = new ArrayList<String>();
- dayItems = new ArrayList<String>();
-
- String date = Utility.dateFormatDay(new Date());
- years = date.split("-")[0];
- month = date.split("-")[1];
- day = date.split("-")[2];
-
- for(int i = 2000; i < 2030; i++){
- yearsItems.add(i + "");
- }
- for(int i = 0; i < 12; i++){
- if(1 + i < 10){
- monthItems.add("0" + (1 + i));
- }else{
- monthItems.add(1 + i + "");
- }
- }
- for(int i = 0; i < Integer.valueOf(Utility.getMonthMaxDay(Utility.dateFormat(new Date()))); i++){
- if(1 + i < 10){
- dayItems.add("0" + (1 + i));
- }else{
- dayItems.add(1 + i + "");
- }
- }
- yearsSeletion = Integer.valueOf(date.split("-")[0]) - 2000;
- monthSeletion = Integer.valueOf(date.split("-")[1]) - 1;
- daySeletion = Integer.valueOf(date.split("-")[2]) - 1;
- }
-
- /**
- * Set the Dialog title from resource
- *
- * @param title
- * @return
- */
- public Builder setTitle(int title) {
- this.title = (String) context.getText(title);
- return this;
- }
-
- /**
- * Set the Dialog title from String
- *
- * @param title
- * @return
- */
-
- public Builder setTitle(String title) {
- this.title = title;
- return this;
- }
-
- public Builder setYearsItems(List<String> list){
- this.yearsItems = list;
- return this;
- }
-
- public Builder setMonthItems(List<String> list){
- this.monthItems = list;
- return this;
- }
-
- public Builder setTextView(TextView tv) {
- this.textView = tv;
- return this;
- }
-
- /**
- * Set the positive button resource and it's listener
- *
- * @param positiveButtonText
- * @return
- */
- public Builder setPositiveButton(int positiveButtonText,
- DialogInterface.OnClickListener listener) {
- this.positiveButtonClickListener = listener;
- return this;
- }
-
- public Builder setPositiveButton(String positiveButtonText,
- DialogInterface.OnClickListener listener) {
- this.positiveButtonClickListener = listener;
- return this;
- }
-
- public Builder setNegativeButton(int negativeButtonText,
- DialogInterface.OnClickListener listener) {
- this.negativeButtonClickListener = listener;
- return this;
- }
-
- public Builder setNegativeButton(String negativeButtonText,
- DialogInterface.OnClickListener listener) {
- this.negativeButtonClickListener = listener;
- return this;
- }
-
- public DateChooseDialog create() {
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- // instantiate the dialog with the custom Theme
- final DateChooseDialog dialog = new DateChooseDialog(context, R.style.Dialog);
- View layout = inflater.inflate(R.layout.date_choose_dialog, null);
- tvTime = (TextView) layout.findViewById(R.id.title);
- wheelView1 = (WheelView)layout.findViewById(R.id.wheelview1);
- wheelView2 = (WheelView)layout.findViewById(R.id.wheelview2);
- wheelView3 = (WheelView)layout.findViewById(R.id.wheelview3);
- positiveButton = (Button) layout.findViewById(R.id.positiveButton);
- negativeButton = (Button) layout.findViewById(R.id.negativeButton);
-
- dialog.addContentView(layout, new LayoutParams(
- LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
- // set the dialog title
- tvTime.setText(title);
- // set the confirm button
- positiveButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- textView.setText(years + "-"
- + month + "-"
- + day);
-
- positiveButtonClickListener.onClick(dialog,
- DialogInterface.BUTTON_POSITIVE);
- }
- });
- negativeButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- negativeButtonClickListener.onClick(dialog,
- DialogInterface.BUTTON_NEGATIVE);
- }
- });
-
- // set the content message
- wheelView1.setOffset(1);
- wheelView1.setItems(yearsItems);
- wheelView1.setSeletion(yearsSeletion);
- wheelView1.setOnWheelViewListener(new OnWheelViewListener(){
- public void onSelected(int selectedIndex, String item) {
- years = item;
- wheelView2.setItems(monthItems);
- ArrayList<String> items = new ArrayList<String>();
- for(int i = 0; i < Integer.valueOf(Utility.getMonthMaxDay(years + "-" + month)); i++){
- if(1 + i < 10){
- items.add("0" + (1 + i));
- }else{
- items.add(1 + i + "");
- }
- }
- wheelView3.setItems(items);
- }
- });
-
- wheelView2.setOffset(1);
- wheelView2.setItems(monthItems);
- wheelView2.setSeletion(monthSeletion);
- wheelView2.setOnWheelViewListener(new OnWheelViewListener(){
- public void onSelected(int selectedIndex, String item) {
- month = item;
- int maxDay = Integer.valueOf(Utility.getMonthMaxDay(years + "-" + item));
- ArrayList<String> items = new ArrayList<String>();
- for(int i = 0; i < maxDay; i++){
- if(1 + i < 10){
- items.add("0" + (1 + i));
- }else{
- items.add(1 + i + "");
- }
- }
- wheelView3.setItems(items);
- if(Integer.valueOf(day) > maxDay){
- day = maxDay + "";
- wheelView3.setSeletion(maxDay);
- }
- }
- });
-
- wheelView3.setOffset(1);
- wheelView3.setItems(dayItems);
- wheelView3.setSeletion(daySeletion);
- wheelView3.setOnWheelViewListener(new OnWheelViewListener(){
- public void onSelected(int selectedIndex, String item) {
- day = item;
- }
- });
-
- dialog.setContentView(layout);
- return dialog;
- }
- }
- }
弹出日期选择框代码
- package com.cx.datechoosedialog;
-
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
-
- public class MainActivity extends ActionBarActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- findViewById(R.id.text).setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- DateChooseDialog.Builder builder = new DateChooseDialog.Builder(MainActivity.this);
- builder.setTitle("选择日期");
- builder.setTextView((TextView)findViewById(R.id.text));
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- //设置你的操作事项
- }
- });
-
- builder.setNegativeButton("取消",
- new android.content.DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- builder.create().show();
- }
- });
- }
-
- }
效果图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。