2017-08-28 139 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();} 

希望你有想法...