Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增选中项的背景色及分割线的宽度 #754

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
Expand Down Expand Up @@ -210,6 +211,9 @@ public void onTimeSelectChanged(Date date) {
})
.setType(new boolean[]{true, true, true, true, true, true})
.isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。
.setDividerWidth(5)//设置选中条目分割线的线宽,单位px,若不设置默认为1
.setDividerColor(Color.RED)
.setSelectItemBgColor(Color.GREEN)//设置选中条目的背景色,若不设置默认为0xFFe7e7e7
.addOnCancelClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ public TimePickerBuilder setDividerColor(@ColorInt int dividerColor) {
return this;
}

/**
* 设置分割线的宽度
*
* @param dividerWidth
*/
public TimePickerBuilder setDividerWidth(int dividerWidth) {
mPickerOptions.dividerWidth = dividerWidth;
return this;
}

/**
* 设置选中条目的颜色
*
* @param selectItemBgColor
*/
public TimePickerBuilder setSelectItemBgColor(int selectItemBgColor) {
mPickerOptions.selectItemBgColor = selectItemBgColor;
return this;
}

/**
* 设置分割线的类型
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public PickerOptions(int buildType) {
public int textColorOut = 0xFFa8a8a8; //分割线以外的文字颜色
public int textColorCenter = 0xFF2a2a2a; //分割线之间的文字颜色
public int dividerColor = 0xFFd5d5d5; //分割线的颜色
public int dividerWidth = 1; //分割线的宽度
public int selectItemBgColor = 0xFFe7e7e7; //选中条目的背景色
public int backgroundId = -1; //显示时的外部背景色颜色,默认是灰色
public int outSideColor = -1; //显示时的外部背景色颜色,默认是灰色

public float lineSpacingMultiplier = 1.6f; // 条目间距倍数 默认1.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public void onTimeSelectChanged() {
wheelTime.setCyclic(mPickerOptions.cyclic);
wheelTime.setDividerColor(mPickerOptions.dividerColor);
wheelTime.setDividerType(mPickerOptions.dividerType);
wheelTime.setDividerWidth(mPickerOptions.dividerWidth);
wheelTime.setSelectItemBgColor(mPickerOptions.selectItemBgColor);
wheelTime.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier);
wheelTime.setTextColorOut(mPickerOptions.textColorOut);
wheelTime.setTextColorCenter(mPickerOptions.textColorCenter);
Expand Down
143 changes: 140 additions & 3 deletions pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public class WheelTime {

private int textSize;

//文字的颜色和分割线的颜色、宽度、选中条目背景色
private int textColorOut;
private int textColorCenter;
private int dividerColor;
private int dividerWidth;
private int selectItemBgColor;

private float lineSpacingMultiplier;
private WheelView.DividerType dividerType;
private boolean isLunarCalendar = false;
private ISelectTimeCallback mSelectChangeCallback;

Expand All @@ -56,6 +65,8 @@ public WheelTime(View view, boolean[] type, int gravity, int textSize) {
this.type = type;
this.gravity = gravity;
this.textSize = textSize;
setView(view);
}
}

public void setLunarMode(boolean isLunarCalendar) {
Expand Down Expand Up @@ -101,14 +112,12 @@ private void setLunar(int year, final int month, int day, boolean isLeap, int h,
wv_month = (WheelView) view.findViewById(R.id.month);
wv_month.setAdapter(new ArrayWheelAdapter(ChinaDate.getMonths(year)));
wv_month.setLabel("");

int leapMonth = ChinaDate.leapMonth(year);
if (leapMonth != 0 && (month > leapMonth - 1 || isLeap)) { //选中月是闰月或大于闰月
wv_month.setCurrentItem(month + 1);
} else {
wv_month.setCurrentItem(month);
}

wv_month.setGravity(gravity);

// 日
Expand Down Expand Up @@ -153,7 +162,6 @@ public void onItemSelected(int index) {
} else {
wv_month.setCurrentItem(wv_month.getCurrentItem());
}

int currentIndex = wv_day.getCurrentItem();
int maxItem = 29;
if (ChinaDate.leapMonth(year_num) != 0 && wv_month.getCurrentItem() > ChinaDate.leapMonth(year_num) - 1) {
Expand All @@ -169,6 +177,7 @@ public void onItemSelected(int index) {
maxItem = ChinaDate.monthDays(year_num, wv_month.getCurrentItem() + 1);
}

if (wv_day.getCurrentItem() > maxItem - 1) {
if (currentIndex > maxItem - 1) {
wv_day.setCurrentItem(maxItem - 1);
}
Expand Down Expand Up @@ -200,6 +209,7 @@ public void onItemSelected(int index) {
maxItem = ChinaDate.monthDays(year_num, month_num + 1);
}

if (wv_day.getCurrentItem() > maxItem - 1) {
if (currentIndex > maxItem - 1) {
wv_day.setCurrentItem(maxItem - 1);
}
Expand Down Expand Up @@ -288,6 +298,7 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) {
wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay));
} else {
// 闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
if (leapYear) {
if (endDay > 29) {
endDay = 29;
Expand All @@ -310,6 +321,14 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) {

wv_day.setAdapter(new NumericWheelAdapter(startDay, 30));
} else {
// 闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

wv_day.setAdapter(new NumericWheelAdapter(startDay, 29));
} else {

wv_day.setAdapter(new NumericWheelAdapter(startDay, 28));
}
// 闰年 29,平年 28
wv_day.setAdapter(new NumericWheelAdapter(startDay, leapYear ? 29 : 28));
}
Expand All @@ -328,6 +347,7 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) {
wv_day.setAdapter(new NumericWheelAdapter(1, endDay));
} else {
// 闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
if (leapYear) {
if (endDay > 29) {
endDay = 29;
Expand All @@ -344,6 +364,20 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) {
} else {
// 判断大小月及是否闰年,用来确定"日"的数据
if (list_big.contains(String.valueOf(month + 1))) {

wv_day.setAdapter(new NumericWheelAdapter(1, 31));
} else if (list_little.contains(String.valueOf(month + 1))) {

wv_day.setAdapter(new NumericWheelAdapter(1, 30));
} else {
// 闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

wv_day.setAdapter(new NumericWheelAdapter(1, 29));
} else {

wv_day.setAdapter(new NumericWheelAdapter(1, 28));
}
wv_day.setAdapter(new NumericWheelAdapter(1, 31));
} else if (list_little.contains(String.valueOf(month + 1))) {
wv_day.setAdapter(new NumericWheelAdapter(1, 30));
Expand Down Expand Up @@ -582,6 +616,71 @@ private void setContentTextSize() {
}


private void setTextColorOut() {
wv_day.setTextColorOut(textColorOut);
wv_month.setTextColorOut(textColorOut);
wv_year.setTextColorOut(textColorOut);
wv_hours.setTextColorOut(textColorOut);
wv_minutes.setTextColorOut(textColorOut);
wv_seconds.setTextColorOut(textColorOut);
}

private void setTextColorCenter() {
wv_day.setTextColorCenter(textColorCenter);
wv_month.setTextColorCenter(textColorCenter);
wv_year.setTextColorCenter(textColorCenter);
wv_hours.setTextColorCenter(textColorCenter);
wv_minutes.setTextColorCenter(textColorCenter);
wv_seconds.setTextColorCenter(textColorCenter);
}

private void setDividerColor() {
wv_day.setDividerColor(dividerColor);
wv_month.setDividerColor(dividerColor);
wv_year.setDividerColor(dividerColor);
wv_hours.setDividerColor(dividerColor);
wv_minutes.setDividerColor(dividerColor);
wv_seconds.setDividerColor(dividerColor);
}

private void setDividerWidth() {
wv_day.setDividerWidth(dividerWidth);
wv_month.setDividerWidth(dividerWidth);
wv_year.setDividerWidth(dividerWidth);
wv_hours.setDividerWidth(dividerWidth);
wv_minutes.setDividerWidth(dividerWidth);
wv_seconds.setDividerWidth(dividerWidth);
}

private void setSelectItemBgColor() {
wv_day.setSelectItemBgColor(selectItemBgColor);
wv_month.setSelectItemBgColor(selectItemBgColor);
wv_year.setSelectItemBgColor(selectItemBgColor);
wv_hours.setSelectItemBgColor(selectItemBgColor);
wv_minutes.setSelectItemBgColor(selectItemBgColor);
wv_seconds.setSelectItemBgColor(selectItemBgColor);
}

private void setDividerType() {

wv_day.setDividerType(dividerType);
wv_month.setDividerType(dividerType);
wv_year.setDividerType(dividerType);
wv_hours.setDividerType(dividerType);
wv_minutes.setDividerType(dividerType);
wv_seconds.setDividerType(dividerType);

}

private void setLineSpacingMultiplier() {
wv_day.setLineSpacingMultiplier(lineSpacingMultiplier);
wv_month.setLineSpacingMultiplier(lineSpacingMultiplier);
wv_year.setLineSpacingMultiplier(lineSpacingMultiplier);
wv_hours.setLineSpacingMultiplier(lineSpacingMultiplier);
wv_minutes.setLineSpacingMultiplier(lineSpacingMultiplier);
wv_seconds.setLineSpacingMultiplier(lineSpacingMultiplier);
}

public void setLabels(String label_year, String label_month, String label_day, String label_hours, String label_mins, String label_seconds) {
if (isLunarCalendar) {
return;
Expand Down Expand Up @@ -622,6 +721,9 @@ public void setLabels(String label_year, String label_month, String label_day, S

public void setTextXOffset(int x_offset_year, int x_offset_month, int x_offset_day,
int x_offset_hours, int x_offset_minutes, int x_offset_seconds) {
wv_day.setTextXOffset(x_offset_year);
wv_month.setTextXOffset(x_offset_month);
wv_year.setTextXOffset(x_offset_day);
wv_year.setTextXOffset(x_offset_year);
wv_month.setTextXOffset(x_offset_month);
wv_day.setTextXOffset(x_offset_day);
Expand Down Expand Up @@ -720,6 +822,10 @@ public View getView() {
return view;
}

public void setView(View view) {
this.view = view;
}

public int getStartYear() {
return startYear;
}
Expand Down Expand Up @@ -800,6 +906,8 @@ public void setRangDate(Calendar startDate, Calendar endDate) {
* @param lineSpacingMultiplier
*/
public void setLineSpacingMultiplier(float lineSpacingMultiplier) {
this.lineSpacingMultiplier = lineSpacingMultiplier;
setLineSpacingMultiplier();
wv_day.setLineSpacingMultiplier(lineSpacingMultiplier);
wv_month.setLineSpacingMultiplier(lineSpacingMultiplier);
wv_year.setLineSpacingMultiplier(lineSpacingMultiplier);
Expand All @@ -814,6 +922,28 @@ public void setLineSpacingMultiplier(float lineSpacingMultiplier) {
* @param dividerColor
*/
public void setDividerColor(int dividerColor) {
this.dividerColor = dividerColor;
setDividerColor();
}

/**
* 设置分割线的宽度
*
* @param dividerWidth
*/
public void setDividerWidth(int dividerWidth) {
this.dividerWidth = dividerWidth;
setDividerWidth();
}

/**
* 设置选中条目的背景色
*
* @param selectItemBgColor
*/
public void setSelectItemBgColor(int selectItemBgColor) {
this.selectItemBgColor = selectItemBgColor;
setSelectItemBgColor();
wv_day.setDividerColor(dividerColor);
wv_month.setDividerColor(dividerColor);
wv_year.setDividerColor(dividerColor);
Expand All @@ -828,6 +958,8 @@ public void setDividerColor(int dividerColor) {
* @param dividerType
*/
public void setDividerType(WheelView.DividerType dividerType) {
this.dividerType = dividerType;
setDividerType();
wv_day.setDividerType(dividerType);
wv_month.setDividerType(dividerType);
wv_year.setDividerType(dividerType);
Expand All @@ -842,6 +974,8 @@ public void setDividerType(WheelView.DividerType dividerType) {
* @param textColorCenter
*/
public void setTextColorCenter(int textColorCenter) {
this.textColorCenter = textColorCenter;
setTextColorCenter();
wv_day.setTextColorCenter(textColorCenter);
wv_month.setTextColorCenter(textColorCenter);
wv_year.setTextColorCenter(textColorCenter);
Expand All @@ -856,6 +990,8 @@ public void setTextColorCenter(int textColorCenter) {
* @param textColorOut
*/
public void setTextColorOut(int textColorOut) {
this.textColorOut = textColorOut;
setTextColorOut();
wv_day.setTextColorOut(textColorOut);
wv_month.setTextColorOut(textColorOut);
wv_year.setTextColorOut(textColorOut);
Expand All @@ -879,6 +1015,7 @@ public void isCenterLabel(boolean isCenterLabel) {
public void setSelectChangeCallback(ISelectTimeCallback mSelectChangeCallback) {
this.mSelectChangeCallback = mSelectChangeCallback;
}
}

public void setItemsVisible(int itemsVisibleCount) {
wv_day.setItemsVisibleCount(itemsVisibleCount);
Expand Down