项目中遇到这样的一个功能:
选择周一,周二,周三,然后把这些值放到text上去,选择了全部就把所有值替换成全部,且这些时间都是按排序排的,看起来很简单一个功能,但我差不多花了一天时间。
效果如下:
我先讲一下我的思路,因为后面从周一到周日是要按顺序来排列的,所以我就创建了一如下一个星期类,里面有position属性用于最后来比较排序。
/**
*
* @todo:创建星期类
* @date:2016-1-12 下午3:03:39
* @author:hg_liuzl@163.com
*/
public static class WeekBean{
public int position;
public String week;
public WeekBean(int position, String week) {
super();
this.position = position;
this.week = week;
}
}
/**
* 比较器排序
*/
private Comparator<WeekBean> compare = new Comparator<WeekBean>() {
@Override
public int compare(WeekBean w1, WeekBean w2) {
return w1.position - w2.position;
}
};
/**
*
* @todo:赋值,排序
* @date:2016-1-12 下午2:35:55
* @author:hg_liuzl@163.com
* @params:
*/
private void setValue() {
if (selectWeekBeans.size() >= BTN_TYPE_ALL) {//如果选中的数量大于或等于7个,我们可以知道这个时候应该显示全部,后面流程都不需要走了
tvWeek.setText("全部");
return;
}
Collections.sort(selectWeekBeans, compare);//把集合里面的顺序重新排一下,周一到周日的顺序
StringBuilder sb = new StringBuilder();
for (WeekBean bean : selectWeekBeans) {
sb.append(bean.week).append(" ");//遍历数据,放到textView中
}
tvWeek.setText(sb.toString());
}
1。把固定的数据放到集合中
2.把按钮控件也放到集合中,以后后面显示选中与未选中的状态
3.每点击一个按钮,直接从固定集合中取出对象,
public class MainActivity extends Activity implements OnClickListener {
/***全选按钮,全部**/
private static final int BTN_TYPE_ALL = 7;
/***固定的数据,这里面不会操作**/
private List<WeekBean> fixedBeans = new ArrayList<WeekBean>();
/** 星期**/
private List<Button> weekBtns= new ArrayList<Button>();
/** 星期**/
private List<WeekBean> selectWeekBeans= new ArrayList<WeekBean>();
private TextView tvWeek;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvWeek = (TextView) findViewById(R.id.tv_week);
initData();
initWeeks();
}
private void initData() {
fixedBeans.add(new WeekBean(0, "周一"));
fixedBeans.add(new WeekBean(1, "周二"));
fixedBeans.add(new WeekBean(2, "周三"));
fixedBeans.add(new WeekBean(3, "周四"));
fixedBeans.add(new WeekBean(4, "周五"));
fixedBeans.add(new WeekBean(5, "周六"));
fixedBeans.add(new WeekBean(6, "周日"));
fixedBeans.add(new WeekBean(7, "全部"));
}
private void initWeeks() {
Button btn = (Button) findViewById(R.id.btn_Monday);
btn.setOnClickListener(this);
weekBtns.add(btn);
btn= (Button) findViewById(R.id.btn_Tuesday);
btn.setOnClickListener(this);
weekBtns.add(btn);
btn= (Button) findViewById(R.id.btn_Wednesday);
btn.setOnClickListener(this);
weekBtns.add(btn);
btn= (Button) findViewById(R.id.btn_Thursday);
btn.setOnClickListener(this);
weekBtns.add(btn);
btn= (Button) findViewById(R.id.btn_Friday);
btn.setOnClickListener(this);
weekBtns.add(btn);
btn= (Button) findViewById(R.id.btn_Saturday);
btn.setOnClickListener(this);
weekBtns.add(btn);
btn= (Button) findViewById(R.id.btn_sunday);
btn.setOnClickListener(this);
weekBtns.add(btn);
btn= (Button) findViewById(R.id.btn_AllDay);
btn.setOnClickListener(this);
weekBtns.add(btn);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_Monday:
switchTab(fixedBeans.get(0));
break;
case R.id.btn_Tuesday:
switchTab(fixedBeans.get(1));
break;
case R.id.btn_Wednesday:
switchTab(fixedBeans.get(2));
break;
case R.id.btn_Thursday:
switchTab(fixedBeans.get(3));
break;
case R.id.btn_Friday:
switchTab(fixedBeans.get(4));
break;
case R.id.btn_Saturday:
switchTab(fixedBeans.get(5));
break;
case R.id.btn_sunday:
switchTab(fixedBeans.get(6));
break;
case R.id.btn_AllDay:
switchTab(fixedBeans.get(7));
break;
default:
break;
}
}
private void switchTab(WeekBean weekBean) {
if (weekBean.position == BTN_TYPE_ALL) { // 为全选按钮
if (selectWeekBeans.contains(weekBean)) { // 如果存在,就删除当前对象
selectWeekBeans.clear();
for (int i = 0; i < weekBtns.size(); i++) {
weekBtns.get(i).setSelected(false);
}
} else {// 不存在就添加一下
selectWeekBeans.clear();
selectWeekBeans.addAll(fixedBeans);
for (int i = 0; i < weekBtns.size(); i++) {
weekBtns.get(i).setSelected(true);
}
}
}else{ // 不是全选按钮
if (selectWeekBeans.size() >= weekBtns.size()-1) { // 如果是全选状态
weekBtns.get(BTN_TYPE_ALL).setSelected(false);
selectWeekBeans.remove(fixedBeans.get(BTN_TYPE_ALL));
weekBtns.get(weekBean.position).setSelected(false);
selectWeekBeans.remove(weekBean);
} else { // 如果不是全选状态
if (selectWeekBeans.contains(weekBean)) { // 如果存在,就删除当前对象
weekBtns.get(weekBean.position).setSelected(false);
selectWeekBeans.remove(weekBean);
} else {// 不存在就添加一下
if (weekBean.position < BTN_TYPE_ALL && selectWeekBeans.size() == 6) {//加上全选按钮
weekBtns.get(BTN_TYPE_ALL).setSelected(true);
selectWeekBeans.add(fixedBeans.get(BTN_TYPE_ALL));
}
weekBtns.get(weekBean.position).setSelected(true);
selectWeekBeans.add(weekBean);
}
}
}
setValue();
}