2017-04-26 178 views
-2

我想用一個按鈕做一個顏色變化,永久使用SharedPreferences。這裏是我的代碼:使用SharedPreferencers Android更改按鈕顏色?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gunawangsa); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle("Gunawangsa Hotel"); 

    button1 = (Button) findViewById(R.id.gu_button); 

    sharedPreferences = getSharedPreferences("ButtonDisable", Context.MODE_PRIVATE); 

    final boolean isEnable = sharedPreferences.getBoolean("ButtonDisable", true); 
    button1.setEnabled(isEnable); 
    button1.setBackgroundColor(Color.RED); 
    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if (button1.isClickable()) { 
       Toast.makeText(getBaseContext(),"Claim", 
         Toast.LENGTH_SHORT).show(); 
       boolean isEnable = sharedPreferences.edit().putBoolean("ButtonDisable", false).commit(); 
       if (isEnable == false) { 
        Toast.makeText(getBaseContext(),"Claim", 
          Toast.LENGTH_SHORT).show(); 
       } else { 
        button1.setBackgroundColor(Color.RED); 
       } 
      } 


     } 


    }); 

} 
+0

實際上你想要什麼? – FAT

+0

我想如果按鈕被點擊然後按鈕永久改變? –

+0

永久禁用或想更改顏色? – FAT

回答

0

試試這個:

import android.content.Context; 
import android.content.SharedPreferences; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 


public class GunawangsaActivity extends AppCompatActivity { 

    Button button1; 

    boolean isEnable; 

    SharedPreferences sharedPreferences; 
    SharedPreferences.Editor editor; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gunawangsa); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setTitle("Gunawangsa Hotel"); 

     button1 = (Button) findViewById(R.id.gu_button); 

     sharedPreferences = getSharedPreferences("ButtonDisable", Context.MODE_PRIVATE); 
     editor = sharedPreferences.edit(); 

     isEnable = sharedPreferences.getBoolean("ButtonDisable", true); 

     if (isEnable) { 
      button1.setEnabled(true); 
      button1.setBackgroundColor(Color.RED); 
     } else { 
      button1.setEnabled(false); 
      button1.setBackgroundColor(Color.GRAY); 
     } 

     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Toast.makeText(getBaseContext(),"Claim", Toast.LENGTH_SHORT).show(); 

       editor.putBoolean("ButtonDisable", false); 
       editor.commit(); 

       button1.setEnabled(false); 
       button1.setBackgroundColor(Color.GRAY); 
      } 
     }); 
    } 
} 

OUTPUT:

enter image description here

希望這將有助於〜

+0

謝謝你這麼多兄弟,它的工作:) –

+0

我的榮幸:) – FAT

1

更換

boolean isEnable = sharedPreferences.edit().putBoolean("ButtonDisable", false).commit(); 

boolean isEnable = sharedPreferences.getBoolean("ButtonDisable", false); 
sharedPreferences.edit().putBoolean("ButtonDisable",boolValue).apply(); 
+0

謝謝,但如果我嘗試關閉我的應用程序,我的按鈕顏色回第一顏色。布爾值下面的 –

+0

isEnable = sharedPreferences.getBoolean(「ButtonDisable」,false); line add this:sharedPreferences.edit()。putBoolean(「ButtonDisable」,boolvalue).apply(); –

0

那麼你這樣做:

button1.setBackgroundColor(Color.RED); 

不管是什麼 'isEnable' 的價值是。

嘗試

button1.setBackgroundColor(isEnable ? Color.RED : Color.WHITE);