Android studio 单击按钮后将计数器保存到共享首选项,但不会';救不了

Android studio 单击按钮后将计数器保存到共享首选项,但不会';救不了,android-studio,sharedpreferences,saving-data,Android Studio,Sharedpreferences,Saving Data,我正在尝试使用按钮增加计数器,并将计数器保存到共享首选项,以便即使退出应用程序,计数器值也不会重置为0。我曾尝试使用共享首选项,但该值无法保存。我看过其他类似的表单,但不知道如何在我的代码版本中实现它 代码如下: 导入androidx.appcompat.app.appcompat活动; 导入android.content.Context; 导入android.content.SharedReferences; 导入android.os.Bundle; 导入androidx.appcompat.

我正在尝试使用按钮增加计数器,并将计数器保存到共享首选项,以便即使退出应用程序,计数器值也不会重置为0。我曾尝试使用共享首选项,但该值无法保存。我看过其他类似的表单,但不知道如何在我的代码版本中实现它

代码如下:

导入androidx.appcompat.app.appcompat活动;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入androidx.appcompat.app.AlertDialog;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
公共类Screen2扩展了AppCompative活动{
文本视图显示值;
int计数器=0;
SharedReferences SharedReferences;
公共静态最终字符串MyPREFERENCES=“MyPrefs”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_屏幕2);
showValue=findViewById(R.id.AnnieCount);
//showValue.setVisibility(视图.不可见);
SharedReferences=GetSharedReferences(MyPREFERENCES,
上下文。模式(私人);
公共无效金额(视图五){
//增加计数
计数器++;
showValue.setText(整数.toString(计数器));
SharedReferences.Editor=SharedReferences.edit();
编辑器.putInt(“安妮”,Integer.valueOf(计数器));
//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()
。在
SharedReferences
编辑器中所做的所有更改都不会存储在
SharedReferences
中,直到您调用
commit()
apply()
。您可以从中阅读更多信息

以下是您可以修复它的方法:

public void AnCount(视图五){
//增加计数
计数器++;
showValue.setText(整数.toString(计数器));
SharedReferences.Editor=SharedReferences.edit();
编辑器.putInt(“安妮”,Integer.valueOf(计数器));
commit();
//openDialog();
}
要查看
SharedReferences
中的存储值,可以使用
SharedReferences.getInt(“计数器”,0)
获取存储值:

@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showValue=findViewById(R.id.AnnieCount);
//showValue.setVisibility(视图.不可见);
SharedReferences=GetSharedReferences(MyPREFERENCES,Context.MODE\u PRIVATE);
计数器=SharedReferences.getInt(“计数器”,0);
showValue.setText(“+计数器);
}

谢谢您的帮助!我已经添加了editor.commit();但似乎该值仍然没有存储。editor.putInt(“annie”,Integer.valueOf(counter));-看起来很正确。你把AnCount方法称为哪里?还有,你从哪里得到SharedReferences的最新值?AnCount是分配给同一个XML中的按钮的OnClick方法不太清楚“SharedReferences的最新值”是什么意思对不起,我对android编程很陌生