2016-09-10 66 views
0

我有按鈕在片段中,我已設置按鈕的初始顏色是綠色和按鈕單擊後我將按鈕顏色設置爲紅色但是當我滑動並去其他片段後來在第一個片段的按鈕顏色設置初始顏色不是紅色請幫助,也是在關閉我的應用程序並重新啓動應用程序的顏色是初始顏色。設置按鈕背景顏色在按鈕單擊後的片段

這是我的代碼對於按鈕,點擊

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 

     case R.id.table1: 

       btnColor = (ColorDrawable) table1.getBackground(); 
       colorId = btnColor.getColor(); 
       if (colorId == getResources().getColor(R.color.colorAcce)) { 
        String value = table1.getText().toString(); 
        Intent intent = new Intent(getActivity(), TakeOrderActivity.class); 
        intent.putExtra("myString", value); 
        startActivity(intent); 
        table1.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent)); 

      }else 
      { 
       ((MainActivity)getActivity()).navigateFragment(1); 
      } 

      break; 
     case R.id.table2: 
      btnColor = (ColorDrawable) table2.getBackground(); 
      colorId = btnColor.getColor(); 
      if (colorId == getResources().getColor(R.color.colorAcce)){ 
      String value1 = table2.getText().toString(); 
      Intent intent1 = new Intent(getActivity(), TakeOrderActivity.class); 
      intent1.putExtra("myString", value1); 
      startActivity(intent1); 
      table2.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent)); 
      }else 
      { 
       ((MainActivity)getActivity()).navigateFragment(1); 
      } 
      break; 
} 

這是編輯的代碼

public String getButtonState(int id) { 
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
    return pref.getString("btn"+id, "not_clicked"); 
} 

public void setButtonState(int id,String state) { 
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
    SharedPreferences.Editor editor = pref.edit(); 
    editor.putString("btn"+id, state); 
    editor.commit(); 
} 
@Override 
public void onResume() { 
    if(getButtonState(R.id.table1).equals("clicked")){ 
     table1.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAcce)); 
    } else { 
     table1.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent)); 

    } 
    if(getButtonState(R.id.table2).equals("clicked")){ 
     table2.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAcce)); 
    } else { 
     table2.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent)); 
    } 
    if(getButtonState(R.id.table9).equals("clicked")){ 
     table9.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent)); 
    } else { 
     table9.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAcce)); 
    } 
    if(getButtonState(R.id.table11).equals("clicked")){ 
     table11.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent)); 
    } else { 
     table11.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAcce)); 
    } 
    super.onResume(); 
} 
+0

你必須將按鈕的顏色保存在內存中,否則Android不知道你是否改變了按鈕的顏色。 –

+0

我可以使用共享首選項來存儲顏色。 – Dipak

+0

使用saveinstance保存fragmnet狀態 –

回答

0

爲此,您需要保存在存儲按鈕的狀態。我建議你在使用sharedPreference SharedPreferences

簡單的解決方法爲

  1. 存儲按鈕狀態的方法setButtonState(R.id.button1, "clicked")
  2. 現在每次調用onResume()時,都會將SharedPreferences中的fragment load值調用。

    public String getButtonState(int id) { 
        SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
        return pref.getString("btn"+id, "not_clicked"); 
    } 
    
    public void setButtonState(int id,String state) { 
        SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
        Editor editor = pref.edit(); 
        editor.putString("btn"+id, state); 
        editor.commit(); 
    } 
    
  3. 現在的onResume寫這篇文章()片段的

    if(getButtonState(R.id.button1).equals("clicked")){ 
        //apply clicked button color 
    } else { 
        //apply another color 
    } 
    
+0

你說的有兩種狀態。當你第一次點擊按鈕時,你需要調用'setButtonState(「clicked」)'。現在,每次你想恢復時,你都必須檢查該按鈕是否已被點擊。如果點擊了,則需要應用紅色否則爲綠色。 –

+0

您需要將button_ID作爲字符串傳遞給密鑰。所以每個按鈕都有其獨特的sharedPreference。我正在編輯問題 –

+0

我已經更新了我的代碼,但在單擊按鈕之前顏色設置爲紅色。 – Dipak

0

當按鈕店點擊按鈕的SharedPreferences的顏色,只要你再次來到同一片段使用該SharedPreferences存儲顏色狀態以將Button的顏色從初始(綠色)更改爲紅色。

在按鈕的點擊:

SharedPreferences preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE); 
Editor editor = preferences.edit(); 
editor = editor.putString("BUTTON_COLOR", "Red").commit(); 

檢查UI知名度:

if(preferences.getString("BUTTON_COLOR", "").equalsIgnorCase("Red")){ 
//Set button color to Red 
}else{ 
//Set button color to green 
} 

例如檢查你的片段。根據你的代碼,當你點擊那個按鈕時,你錯過了設置顏色的狀態。只嘗試獲取onResume中的顏色狀態,您將如何直接設置任何內容。 檢查下面的一個來完成。

//Variables 
private SharedPreferences preferences; 
private Editor editor; 
private Button btnClickMe; 

//In onCreate of fragment 
preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE); 
editor = preferences.edit(); 

//In onCreateView of fragment 
btnClickMe = (Button) findViewById(R.id.btnClickMe); 
btnClickMe.setOnClickListener(this); 

@Override 
    protected void onResume() { 
     super.onResume(); 
     if(preferences.getString("BUTTON_COLOR", "Green").equalsIgnoreCase("Green")){ 
      btnClickMe.setBackgroundColor(getResources().getColor(R.color.green)); 
     }else{ 
      btnClickMe.setBackgroundColor(getResources().getColor(R.color.red)); 
     } 
    } 

@Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.btnClickMe: 
      if((((ColorDrawable)btnClickMe.getBackground()).getColor()) == getResources().getColor(R.color.red)){ 
       //navigate to activity 
       editor.putString("BUTTON_COLOR", "Green").commit(); 
       btnClickMe.setBackgroundColor(getResources().getColor(R.color.green)); 
      }else{ 
       //navigate to fragment 
       editor.putString("BUTTON_COLOR", "Red").commit(); 
       btnClickMe.setBackgroundColor(getResources().getColor(R.color.red)); 
      } 
      break; 
     default: 
      break; 
     } 
    } 
+0

請任何人都給我正確的解決方案,以改變片段刷新後按鈕的顏色 – Dipak

+0

用示例檢查我更新的代碼 –

+0

但是我有很多按鈕,如果我使用上面的代碼設置顏色,它會在點擊前顯示紅色。 – Dipak