保存状态按钮 sharedpreferences Android
Save state button sharedpreferences Android
我有一个浮动按钮(它很快就是一个按钮),当我点击它时,它会启动一项服务,并且按钮会更改图标。我这样试过:
mFab = (FloatingActionButton)findViewById(R.id.fabbutton);
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
mFab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (RunService.isRunning) {
RunService.stopRun(MainActivity.this);
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
SharedPreferences.Editor editor = getSharedPreferences("com.mypck.asd", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.commit();
} else {
startService(new Intent(MainActivity.this, RunService.class));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_pause));
}
}
});
并检索状态:
SharedPreferences sharedPrefs = getSharedPreferences("com.mypck.asd", MODE_PRIVATE);
mFab.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true));
但不能以这种方式工作,因为 setChecked
方法是用于切换按钮的。我的是一个简单的按钮!我该如何解决?
所以只需在 SharedPreference
中保存按钮的两种状态,例如 true 和 false 当服务为 运行 或不在 if-else
条件
看下面的代码。
SharedPreferences.Editor editor = getSharedPreferences("com.mypck.asd", MODE_PRIVATE).edit();
if (RunService.isRunning) {
RunService.stopRun(MainActivity.this);
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
editor.putBoolean("NameOfThingToSave", true);
} else {
startService(new Intent(MainActivity.this, RunService.class));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_pause));
editor.putBoolean("NameOfThingToSave", false);
}
editor.commit();
更新:
要检索值,您必须这样做,
SharedPreferences sharedPrefs = getSharedPreferences("com.mypck.asd", MODE_PRIVATE);
boolean buttonState = sharedPrefs.getBoolean("NameOfThingToSave", true);
if(buttonState)
{
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
}else{
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_pause));
}
替换
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
用我上面的代码。
好的,只需将状态保存在您的偏好中即可。
mFab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = getSharedPreferences("com.mypck.asd", MODE_PRIVATE).edit();
if (RunService.isRunning) {
RunService.stopRun(MainActivity.this);
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
editor.putBoolean("NameOfThingToSave", true);
} else {
startService(new Intent(MainActivity.this,RunService.class));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_pause));
editor.putBoolean("NameOfThingToSave", false);
}
editor.commit();
}
});
我有一个浮动按钮(它很快就是一个按钮),当我点击它时,它会启动一项服务,并且按钮会更改图标。我这样试过:
mFab = (FloatingActionButton)findViewById(R.id.fabbutton);
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
mFab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (RunService.isRunning) {
RunService.stopRun(MainActivity.this);
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
SharedPreferences.Editor editor = getSharedPreferences("com.mypck.asd", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.commit();
} else {
startService(new Intent(MainActivity.this, RunService.class));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_pause));
}
}
});
并检索状态:
SharedPreferences sharedPrefs = getSharedPreferences("com.mypck.asd", MODE_PRIVATE);
mFab.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true));
但不能以这种方式工作,因为 setChecked
方法是用于切换按钮的。我的是一个简单的按钮!我该如何解决?
所以只需在 SharedPreference
中保存按钮的两种状态,例如 true 和 false 当服务为 运行 或不在 if-else
条件
看下面的代码。
SharedPreferences.Editor editor = getSharedPreferences("com.mypck.asd", MODE_PRIVATE).edit();
if (RunService.isRunning) {
RunService.stopRun(MainActivity.this);
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
editor.putBoolean("NameOfThingToSave", true);
} else {
startService(new Intent(MainActivity.this, RunService.class));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_pause));
editor.putBoolean("NameOfThingToSave", false);
}
editor.commit();
更新:
要检索值,您必须这样做,
SharedPreferences sharedPrefs = getSharedPreferences("com.mypck.asd", MODE_PRIVATE);
boolean buttonState = sharedPrefs.getBoolean("NameOfThingToSave", true);
if(buttonState)
{
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
}else{
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_pause));
}
替换
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
用我上面的代码。
好的,只需将状态保存在您的偏好中即可。
mFab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = getSharedPreferences("com.mypck.asd", MODE_PRIVATE).edit();
if (RunService.isRunning) {
RunService.stopRun(MainActivity.this);
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_play));
editor.putBoolean("NameOfThingToSave", true);
} else {
startService(new Intent(MainActivity.this,RunService.class));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_pause));
editor.putBoolean("NameOfThingToSave", false);
}
editor.commit();
}
});