[英]How to uncheck and save state of checkbox with a button press
我正在嘗試制作一個復選框,以保存您單擊它時所在的 state,這樣當您完全關閉應用程序時,當您再次打開應用程序時,它將位於同一個 state 中。 我設法做到了這一點。 我還有一個按鈕,當您單擊它時,該按鈕會將復選框的 state 更改為未選中。 但是當您使用按鈕時,它不會保存復選框的 state。
Java:
public class MainActivity extends AppCompatActivity {
EditText textView;
Button button;
Vibrator vibrator;
CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (EditText) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
//Knapp//
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
String oldItem = sharedPref.getString("oldItem", "Ingenting er lagret enda...");
//Checkbox//
SharedPreferences settings = getSharedPreferences("mysettings", 0);
SharedPreferences.Editor editor = settings.edit();
checkBox.setChecked(settings.getBoolean("checkBox", false));
textView.setText(oldItem);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("oldItem", textView.getText().toString());
editor.apply();
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setChecked(false);
button = findViewById(R.id.button);
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(30);
Animation animation = new AlphaAnimation(1.0f, 0.5f);
animation.setDuration(100);
button.startAnimation(animation);
boolean checkBoxValue = checkBox.isChecked();
editor.putBoolean("checkBox", checkBoxValue);
editor.apply();
}
});
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(20);
boolean checkBoxValue = checkBox.isChecked();
editor.putBoolean("checkBox", checkBoxValue);
editor.apply();
}
});
};
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2A3039"
android:paddingHorizontal="60dp"
tools:context=".MainActivity"
android:clipToPadding="false">
<EditText
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textbox"
android:fontFamily="sans-serif"
android:gravity="center_horizontal|center_vertical"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Hello World!"
android:textColor="#FFFFFF"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.123" />
<Button
android:id="@+id/button"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_marginTop="224dp"
android:background="@drawable/blue_button"
android:fontFamily="sans-serif-black"
android:gravity="center_horizontal|center_vertical"
android:onClick="tapToAnimate"
android:text="Bytt..."
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.34" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#01B075"
android:scaleX="2"
android:scaleY="2"
android:shadowColor="#cf1d1d"
android:shadowDx="0.0"
android:shadowDy="0.0"
android:shadowRadius="8"
android:text="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.072" />
</androidx.constraintlayout.widget.ConstraintLayout>''
任何幫助,將不勝感激
在您的按鈕單擊偵聽器中,您正在從以下位置創建SharedPreferences編輯器:
//Knapp//
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
....
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("oldItem", textView.getText().toString());
editor.apply();
....
boolean checkBoxValue = checkBox.isChecked();
editor.putBoolean("checkBox", checkBoxValue);
editor.apply();
}
});
在復選框偵聽器中,您使用的是從創建的編輯器
//Checkbox//
SharedPreferences settings = getSharedPreferences("mysettings", 0);
如果您的復選框將在創建時被選中,則產生結果:
checkBox.setChecked(settings.getBoolean("checkBox", false));
嘗試刪除按鈕 onClick 中的第一行,這樣它將使用相同的編輯器,或者如果您需要它們兩個,請確保也更新從設置創建的另一個編輯器。
另一方面,您在 class 中創建了元素:
EditText textView;
Button button;
Vibrator vibrator;
CheckBox checkBox;
並在您的onCreate中通過 id 找到它們,不要在每個偵聽器中重新找到它們,只需使用您之前已經聲明過的元素
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.