2016-10-12 115 views
0

我使用textviews作爲列表,單擊每個textview時會打開一個片段。 我想改變textview的背景及其點擊時的顏色。 點擊另一個文本視圖時,以前的背景顏色和文本顏色應該更改爲默認顏色。我正在使用Butterknife將所有textviews綁定到一個數組中。從textviews數組中更改textview顏色

這裏是我的代碼:

public class AccountMenuFragment extends Fragment { 

private accountMenuCallback callback; 
private boolean stateChanged; 

public AccountMenuFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRetainInstance(true); 
    try { 
     callback = (accountMenuCallback) getActivity(); 
    } catch (ClassCastException e) { 
     e.printStackTrace(); 
     throw new ClassCastException("Calling Activity/Fragment must implement DialogClickListener interface"); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_my_account, container, false); 
    ButterKnife.bind(this, view); 
    return view; 
} 

@OnClick({R.id.rlAccount, R.id.rlBusiness, R.id.rlIndustry, 
     R.id.rlVerification, R.id.rlBank, R.id.rlPersonal, R.id.rlPassword}) 
public void changeColor(TextView textView) { 
    stateChanged = !stateChanged; 
    if (stateChanged) { 
     // reset background to default; 
     textView.setTextColor(getResources().getColor(R.color.text_color)); 
     textView.setBackgroundColor(Color.WHITE); 
    } else { 
     textView.setTextColor(Color.WHITE); 
     textView.setBackgroundColor(getResources().getColor(R.color.text_color_blue)); 
    } 
}} 

但改變它在相同的TextView的雙擊顏色。但我想單擊一下即可更改和恢復以前的內容。

+1

'private boolean stateChanged =!stateChanged;'你在這裏做什麼?一個方法內的'private'變量?這甚至編譯? – Leo

+0

private boolean stateChanged;是一個全局聲明的變量 –

+0

你必須在方法中使用'this.varabaleName'這個變量,如果同名變量存在.......... – sushildlh

回答

0

將第一個視圖設置爲默認選中狀態並將其聲明爲lastSelectedView。

public class AccountMenuFragment extends Fragment { 

private accountMenuCallback callback; 
@Bind(R.id.rlAccount) 
TextView lastSelectedView; 

public AccountMenuFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRetainInstance(true); 
    try { 
     callback = (accountMenuCallback) getActivity(); 
    } catch (ClassCastException e) { 
     e.printStackTrace(); 
     throw new ClassCastException("Calling Activity/Fragment must implement DialogClickListener interface"); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_my_account, container, false); 
    ButterKnife.bind(this, view); 
    return view; 
} 

@OnClick({R.id.rlAccount, R.id.rlBusiness, R.id.rlIndustry,R.id.rlVerification, R.id.rlBank, R.id.rlPersonal, R.id.rlPassword}) 
public void changeColorOfTextView(TextView textView) { 
    // reset background to default; 
    lastSelectedView.setTextColor(getResources().getColor(R.color.text_color)); 
    lastSelectedView.setBackgroundColor(Color.WHITE); 

    // change color 
    textView.setTextColor(Color.WHITE); 
    textView.setBackgroundColor(getResources().getColor(R.color.text_color_blue)); 
    lastSelectedView = textView; 
}}