我想用一個按鈕做一個顏色變化,永久使用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);
}
}
}
});
}
實際上你想要什麼? – FAT
我想如果按鈕被點擊然後按鈕永久改變? –
永久禁用或想更改顏色? – FAT