首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码优化问题

代码优化问题
EN

Stack Overflow用户
提问于 2017-08-28 12:57:52
回答 1查看 38关注 0票数 0

我正在Android开发中做一些代码更改,以使我的编码更加准确。我整个星期的布局中都有7 TextViews,我已经找到了所有TextViews的视图。

其要求是,当用户单击该7 TextView中的任何人时,只应选择一个TextViews。

因此,我有一些重复的代码,如下所示,请查看它:

代码语言:javascript
复制
 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;

如,您可以在上面的代码中看到。我已经拿了一个开关箱来处理我所有的七个TextViews的点击,上面的情况是周日的。所以现在,你可能会想到,从星期一到星期六,我在剩下的日子里也做了同样的事情。对,是这样。

现在,我必须优化我的其他部分,因为我已经优化了我的if部分在上面的代码。

怎么做到的?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-28 13:53:39

您必须创建一个类:

WeekDay

代码语言:javascript
复制
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);
}}

然后在主类中插入一个带有WeekDays的数组。

WeekDays[] days=new WeekDays[]{new WeekDay(context,D_SUN,sundeyTxt),new WeekDay(context,D_MON,mondeyTxt),...}

在你的其他地方你可以打电话:

代码语言:javascript
复制
for(int i=0;i<days.length;i++){ if(i==0)days[i].select();else days[i].unselect();}

希望你有这个想法..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45919652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档