2017-08-28 133 views
0

我在Android开发中做了一些代码更改,以使我的编码更加准确。 我有我的布局中的所有星期几天的7个TextViews,我已经通过id为所有这些TextView找到了查看。代码优化问题

要求是,当用户单击来自该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; 

如,你可以看到在上面的代码。我已经采取了开关的情况下处理我的所有七个TextViews的点击,以上情况适用于SUNDAY。 所以,现在,您可能会明白,我在星期一至星期六剩余的日子里完成了相同的工作。正确。

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

怎么样?

在此先感谢。

+0

什么是'mTxtSunday'在你的代码,按钮,textview? –

+0

雅,其实它的TextView ..但是,没关系。按钮和textview的概念是相同的。为我的错误而打招呼。将其视为TextView。 –

+0

什么是'D_SUN'是它的'int','String'的东西? –

回答

0

你必须创建一个类:

平日

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); 
}} 

然后在你的主类的init与平日

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

,并在其他数组你可致电:

for(int i=0;i<days.length;i++){ if(i==0)days[i].select();else days[i].unselect();} 

希望你有想法...