繁体   English   中英

如何通过按下按钮取消选中并保存复选框的 state

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM