使用 SharedPreferences Android 更改按钮颜色?
Change Button color with SharedPreferencers Android?
我想使用 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);
}
}
}
});
}
替换
boolean isEnable = sharedPreferences.edit().putBoolean("ButtonDisable", false).commit();
和
boolean isEnable = sharedPreferences.getBoolean("ButtonDisable", false);
sharedPreferences.edit().putBoolean("ButtonDisable",boolValue).apply();
好吧,你这样做:
button1.setBackgroundColor(Color.RED);
不管 'isEnable' 的值是多少。
尝试
button1.setBackgroundColor(isEnable ? Color.RED : Color.WHITE);
试试这个:
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);
}
});
}
}
输出:
希望对你有所帮助~
我想使用 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);
}
}
}
});
}
替换
boolean isEnable = sharedPreferences.edit().putBoolean("ButtonDisable", false).commit();
和
boolean isEnable = sharedPreferences.getBoolean("ButtonDisable", false);
sharedPreferences.edit().putBoolean("ButtonDisable",boolValue).apply();
好吧,你这样做:
button1.setBackgroundColor(Color.RED);
不管 'isEnable' 的值是多少。
尝试
button1.setBackgroundColor(isEnable ? Color.RED : Color.WHITE);
试试这个:
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);
}
});
}
}
输出:
希望对你有所帮助~