代码优化问题
Code Optimization issue
我正在 Android 开发中进行一些代码更改,以使我的编码更加准确。
我所有工作日的布局中都有 7 个 TextView,我已经完成了所有这些 TextView 的按 ID 查找视图。
要求是当用户单击这 7 个 TextView 中的任何一个时,一次只能保持选中一个 TextView。
所以,我有一些重复的代码如下,检查一下:
case R.id.txt_sunday:
if (Prefrences.getBooleanValue(mContext, D_SUN)) {
doUnSelect(D_SUN, mTxtSunday);
} else {
Prefrences.setBooleanValue(mContext, HH_FILTER, true);
selectedDay = "0";
Prefrences.setBooleanValue(mContext, D_SUN, true);
Prefrences.setBooleanValue(mContext, D_MON, false);
Prefrences.setBooleanValue(mContext, D_TUE, false);
Prefrences.setBooleanValue(mContext, D_WED, false);
Prefrences.setBooleanValue(mContext, D_THR, false);
Prefrences.setBooleanValue(mContext, D_FRI, false);
Prefrences.setBooleanValue(mContext, D_SAT, false);
mTxtSunday.setBackgroundResource(R.color.colorAppDefault);
mTxtSunday.setTextColor(getResources().getColor(R.color.white));
mTxtMonday.setBackgroundResource(R.color.white);
mTxtMonday.setTextColor(getResources().getColor(R.color.black));
mTxtTuesday.setBackgroundResource(R.color.white);
mTxtTuesday.setTextColor(getResources().getColor(R.color.black));
mTxtWednesday.setBackgroundResource(R.color.white);
mTxtWednesday.setTextColor(getResources().getColor(R.color.black));
mTxtThrusday.setBackgroundResource(R.color.white);
mTxtThrusday.setTextColor(getResources().getColor(R.color.black));
mTxtFriday.setBackgroundResource(R.color.white);
mTxtFriday.setTextColor(getResources().getColor(R.color.black));
mTxtSaturday.setBackgroundResource(R.color.white);
mTxtSaturday.setTextColor(getResources().getColor(R.color.black));
}
break;
因为,您可以在上面的代码中看到。我采用了一个切换案例来处理我所有七个 TextView 的点击,上面的案例是针对星期天的。
所以现在,你可能知道我在周一到周六的剩余日子里也做了同样的事情。正确。
现在,我必须优化我的 else 部分,因为我已经优化了上面代码中的 if 部分。
如何?
提前致谢。
您必须创建一个 class:
工作日
public class WeekDay {
final String key;
final TextView textView;
final Context context;
public WeekDay(Context context, String key, TextView textView) {
this.key = key;
this.textView = textView;
this.context = context;
}
public void select() {
textView.setTextColor(context.getResources().getColor(R.color.black));
textView.setBackgroundResource(R.color.black);
Prefrences.setBooleanValue(context, key, true);
}
public void unSelect() {
textView.setTextColor(context.getResources().getColor(R.color.white));
textView.setBackgroundResource(R.color.colorAppDefault);
Prefrences.setBooleanValue(context, key, false);
}}
然后在你的主 class 中初始化一个包含 WeekDays
的数组
WeekDays[] days=new WeekDays[]{new WeekDay(context,D_SUN,sundeyTxt),new WeekDay(context,D_MON,mondeyTxt),...}
在你的 else 中你可以调用:
for(int i=0;i<days.length;i++){ if(i==0)days[i].select();else days[i].unselect();}
希望你明白...
我正在 Android 开发中进行一些代码更改,以使我的编码更加准确。 我所有工作日的布局中都有 7 个 TextView,我已经完成了所有这些 TextView 的按 ID 查找视图。
要求是当用户单击这 7 个 TextView 中的任何一个时,一次只能保持选中一个 TextView。
所以,我有一些重复的代码如下,检查一下:
case R.id.txt_sunday:
if (Prefrences.getBooleanValue(mContext, D_SUN)) {
doUnSelect(D_SUN, mTxtSunday);
} else {
Prefrences.setBooleanValue(mContext, HH_FILTER, true);
selectedDay = "0";
Prefrences.setBooleanValue(mContext, D_SUN, true);
Prefrences.setBooleanValue(mContext, D_MON, false);
Prefrences.setBooleanValue(mContext, D_TUE, false);
Prefrences.setBooleanValue(mContext, D_WED, false);
Prefrences.setBooleanValue(mContext, D_THR, false);
Prefrences.setBooleanValue(mContext, D_FRI, false);
Prefrences.setBooleanValue(mContext, D_SAT, false);
mTxtSunday.setBackgroundResource(R.color.colorAppDefault);
mTxtSunday.setTextColor(getResources().getColor(R.color.white));
mTxtMonday.setBackgroundResource(R.color.white);
mTxtMonday.setTextColor(getResources().getColor(R.color.black));
mTxtTuesday.setBackgroundResource(R.color.white);
mTxtTuesday.setTextColor(getResources().getColor(R.color.black));
mTxtWednesday.setBackgroundResource(R.color.white);
mTxtWednesday.setTextColor(getResources().getColor(R.color.black));
mTxtThrusday.setBackgroundResource(R.color.white);
mTxtThrusday.setTextColor(getResources().getColor(R.color.black));
mTxtFriday.setBackgroundResource(R.color.white);
mTxtFriday.setTextColor(getResources().getColor(R.color.black));
mTxtSaturday.setBackgroundResource(R.color.white);
mTxtSaturday.setTextColor(getResources().getColor(R.color.black));
}
break;
因为,您可以在上面的代码中看到。我采用了一个切换案例来处理我所有七个 TextView 的点击,上面的案例是针对星期天的。 所以现在,你可能知道我在周一到周六的剩余日子里也做了同样的事情。正确。
现在,我必须优化我的 else 部分,因为我已经优化了上面代码中的 if 部分。
如何?
提前致谢。
您必须创建一个 class:
工作日
public class WeekDay {
final String key;
final TextView textView;
final Context context;
public WeekDay(Context context, String key, TextView textView) {
this.key = key;
this.textView = textView;
this.context = context;
}
public void select() {
textView.setTextColor(context.getResources().getColor(R.color.black));
textView.setBackgroundResource(R.color.black);
Prefrences.setBooleanValue(context, key, true);
}
public void unSelect() {
textView.setTextColor(context.getResources().getColor(R.color.white));
textView.setBackgroundResource(R.color.colorAppDefault);
Prefrences.setBooleanValue(context, key, false);
}}
然后在你的主 class 中初始化一个包含 WeekDays
的数组WeekDays[] days=new WeekDays[]{new WeekDay(context,D_SUN,sundeyTxt),new WeekDay(context,D_MON,mondeyTxt),...}
在你的 else 中你可以调用:
for(int i=0;i<days.length;i++){ if(i==0)days[i].select();else days[i].unselect();}
希望你明白...