单击按钮后将计数器保存到共享首选项,但不会保存

Saving a Counter to shared preferences after a button is clicked, but it doesn't save

我正在尝试让一个按钮增加一个计数器并将计数器保存到共享首选项中,这样即使应用程序退出,计数器值也不会重置为 0。我尝试使用共享首选项但是该值只是不会保存。我看过其他类似的表格,但无法理解如何将其实现到我的代码版本中。

这是代码:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Screen2 extends AppCompatActivity {

    TextView showValue;

    int counter = 0;

    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs" ;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen2);

        showValue = findViewById(R.id.AnnieCount);
        //showValue.setVisibility(View.INVISIBLE);

      sharedpreferences = getSharedPreferences(MyPREFERENCES, 
      Context.MODE_PRIVATE);

        public void AnCount(View v) {
            //increase the count
            counter++;
            showValue.setText(Integer.toString(counter));
            SharedPreferences.Editor editor = sharedpreferences.edit ();
            editor.putInt("annie", Integer.valueOf(counter));
            // openDialog();

    }

编辑

这是最低工作代码 & XML :

主要活动:

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView showValue;
    int counter = 0;

    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showValue = findViewById(R.id.AnnieCount);
        //showValue.setVisibility(View.INVISIBLE);

        sharedpreferences = getSharedPreferences(MyPREFERENCES,
                Context.MODE_PRIVATE);


    }


    public void AnCount(View v) {
        //increase the count
        counter++;
        showValue.setText(Integer.toString(counter));
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putInt("counter", counter);
        editor.commit();
        // openDialog();
    }
}

这是 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/AnnieCount"
        android:layout_width="57dp"
        android:layout_height="43dp"
        android:layout_marginStart="12dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="145dp"
        android:layout_marginEnd="167dp"
        android:layout_marginRight="167dp"
        android:gravity="center"
        android:text="0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/AnnieBtn"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/AnnieBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="72dp"
        android:layout_marginLeft="72dp"
        android:layout_marginTop="145dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        android:onClick="AnCount"
        android:text="annie_liou"
        app:layout_constraintEnd_toStartOf="@+id/AnnieCount"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

我需要做的就是即使在完全关闭应用程序后也能存储增量值。

您应该在离开之前致电 editor.commit()。在调用 commit()apply() 之前,您在 SharedPreferences 编辑器中所做的所有更改都不会存储在 SharedPreferences 中。您可以从 here.

阅读更多内容

修复方法如下:

public void AnCount(View v) {
        //increase the count
        counter++;
        showValue.setText(Integer.toString(counter));
        SharedPreferences.Editor editor = sharedpreferences.edit ();
        editor.putInt("annie", Integer.valueOf(counter));
        editor.commit();
        // openDialog();

}

要查看来自SharedPreferences的储值,您可以通过sharedpreferences.getInt("counter", 0)获取储值:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    showValue = findViewById(R.id.AnnieCount);
    //showValue.setVisibility(View.INVISIBLE);

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    counter = sharedpreferences.getInt("counter", 0);

    showValue.setText("" + counter);
}