【发布时间】:2017-04-26 15:39:53
【问题描述】:
我想使用 SharedPreferences 永久更改颜色,通过按钮完成。这是我的代码:
@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);
}
}
}
});
}
【问题讨论】:
-
你到底想要什么?
-
我希望如果单击按钮然后按钮永久更改??
-
永久禁用还是只想更改颜色?
-
是先生禁用并更改颜色先生?
标签: android android-button android-sharedpreferences