java - 使用 android SharedPreferences 在单独的 Activity 中保存和重用值(非常基本的应用程序)

标签 java android android-activity sharedpreferences

我为我的更大问题制作了一个快速测试应用程序,即如何使用 SharedPreferences 在 Activity 之间共享变量。至于研究这个主题,我已经使用 SharedPreferences 一段时间了,并且研究了这个确切的问题,但它进入了一堆“死胡同”(其中代码不适用和/或对于我的情况)。这里的应用程序是一个非常基本的双 View 应用程序,只有(在两个页面上)一个为分数加 1 并正确更新 TextView 的按钮,一个进入第二个 Activity (具有相同选项)的按钮,以及显示分数的实际 TextView。非常感谢任何和所有的帮助。

此外,没有实际错误。目前的问题是,每次 Activity 发生变化时,分数都会重置。

主 Activity (Java):

package com.exampleryancocuzzo.ryan.testsharedpref;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

SharedPreferences pref;
SharedPreferences.Editor editor;
int count;      // I referenced this as the score
TextView textView;

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

     pref = getApplicationContext().getSharedPreferences("MY_PREFS",MODE_PRIVATE);
    editor = pref.edit();
    count = pref.getInt("count", -1);
    if (count==-1){
        count = 0;
    }
    editor.commit();

    textView = (TextView) findViewById(R.id.count);
    textView.setText(count+"");
}

public void act2(View view){  // goes to Activity 2
    editor.putInt("count",count);

    Intent intent = new Intent(MainActivity.this,Main2Activity.class);
    startActivity(intent);
}

public void addC(View view){ // adds to the count variable
    count++;
    textView.setText(count+"");
}

}

Main2Activity(Java):

package com.exampleryancocuzzo.ryan.testsharedpref;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

SharedPreferences pref;
SharedPreferences.Editor editor;
int count;
TextView textView2;

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

    pref = getSharedPreferences("MY_PREFS",MODE_PRIVATE);
    editor = pref.edit();
    editor.commit();

    count = pref.getInt("count", -1);
    if (count==-1){
        count=0;
    }
    textView2 = (TextView) findViewById(R.id.count2);
    textView2.setText(count+"");
}

public void act1(View view){
    editor.putInt("count",count);
    Intent intent = new Intent(Main2Activity.this,MainActivity.class);
    startActivity(intent);
}

public void addC2(View view){
    count++;
    textView2.setText(count + "");
}
}

主要 Activity (XML):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.exampleryancocuzzo.ryan.testsharedpref.MainActivity">

<TextView
    android:id="@+id/count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textSize="60sp"
    android:layout_centerHorizontal="true"/>
<Button
    android:id="@+id/add"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ADD TO COUNT"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="addC"
    />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="next"
    android:onClick="act2"
    android:layout_above="@id/add"
    android:layout_centerHorizontal="true"/>
</RelativeLayout>

Main2Activity (XML):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.exampleryancocuzzo.ryan.testsharedpref.Main2Activity">
<TextView
    android:id="@+id/count2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textSize="60sp"
    android:layout_centerHorizontal="true"/>
<Button
    android:id="@+id/add2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ADD TO COUNT"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="addC2"
    />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="next"
    android:layout_above="@id/add2"
    android:layout_centerHorizontal="true"
    android:onClick="act1"
    />
</RelativeLayout>

最佳答案

您必须在将计数值放入首选项后应用提交,如下所示:

editor.commit();

editor.apply();

然后在第二个 Activity 中使用它

关于java - 使用 android SharedPreferences 在单独的 Activity 中保存和重用值(非常基本的应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36561523/

相关文章:

android - 无法获取用户使用我的 Android 应用程序的国家/地区

Android Bootstrap 和 Android studio 无法添加库

java - 在Java中使用.wav格式循环播放音频文件

java - 如何在数组中创建单独的按钮名称?

android - 其他应用程序上的 float 图标

java - 在另一个 Activity 中使用从主 Activity 实例化的套接字

java - 用Java显示存储在数据库中的图像

java - Keycloak 管理客户端响应 Bad Request 以尝试列出领域

java - Android:使 imageView 在动画上可点击

java - 从android中的堆栈中删除一个 Activity