我在这个.java文件中有一个SharedPreference;在底部,您可以看到我将值保存到SharedPreferences
GB_PREFERENCES_BENCH和GB_PREFERENCES_FLIES。如何在其他活动中使用这些值?请参阅第二个代码示例以了解如何使用它。
package com.creativecoders.gymbuddy;
import com.creativecoders.gymbuddy.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class Benchmark extends Activity {
public static final String GB_PREFERENCES = "Prefs";
public static final String GB_PREFERENCES_BENCH = "Bench";
public static final String GB_PREFERENCES_FLIES = "Flies";
SharedPreferences gBValues;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_benchmark);
gBValues = getSharedPreferences(GB_PREFERENCES, Context.MODE_PRIVATE);
}
public void onStart() {
super.onStart();
findViewById(R.id.button5).setOnClickListener(new handleButton5());
}
class handleButton5 implements OnClickListener {
public void onClick(View v) {
EditText editText1 = (EditText)findViewById(R.id.editText1);
String sWeight = editText1.getText().toString();
final double dWeight = Double.parseDouble(sWeight);
EditText editText2 = (EditText)findViewById(R.id.editText2);
String sPush = editText2.getText().toString();
final double dPush = Double.parseDouble(sPush);
EditText editText3 = (EditText)findViewById(R.id.editText3);
String sSit = editText3.getText().toString();
final double dSit = Double.parseDouble(sSit);
EditText editText4 = (EditText)findViewById(R.id.editText4);
String sPull = editText4.getText().toString();
final double dPull = Double.parseDouble(sPull);
double dBench = (((Math.floor(dWeight*.0664))*10)-10)+dPush;
double dFlies = (Math.floor(((Math.floor(dBench*.6)/10)*10)));
int iBench = (int)dBench;
int iFlies = (int)dFlies;
Editor editor1 = gBValues.edit();
editor1.putInt(GB_PREFERENCES_BENCH, iBench);
editor1.commit();
Editor editor2 = gBValues.edit();
editor2.putInt(GB_PREFERENCES_FLIES, iFlies);
editor2.commit();
}
}
}
这是我要使用的方式;(特别是在on create方法中,将TextView的文本设置为SharePreference中的值)
package com.creativecoders.gymbuddy;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Upper100Start extends Activity {
public static final String GB_PREFERENCES = "Prefs";
public static final String GB_PREFERENCES_CURLS = "Curls";
SharedPreferences gBValues;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upper100start);
if (gBValues.contains(GB_PREFERENCES_CURLS)){
TextView TextView2 = (TextView)findViewById(R.id.textView2);
TextView2.setText(gBValues.getString(GB_PREFERENCES_CURLS, ""));
}
}
public void onStart() {
super.onStart();
findViewById(R.id.button2).setOnClickListener(new handleButton2());
findViewById(R.id.button3).setOnClickListener(new handleButton3());
}
class handleButton2 implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Upper100Start.this, Upper101.class);
startActivity(intent);
}
}
class handleButton3 implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Upper100Start.this, Main.class);
startActivity(intent);
}
}
}
共享首选项可在整个应用程序中访问,因此您可以从应用程序中的任何活动中读取它们。
在活动A中存储键/值对:
SharedPreferences settings = getSharedPreferences("mysettings",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("mystring", "wahay");
editor.commit();
从另一个活动中读取此值:
SharedPreferences settings = getSharedPreferences("mysettings",
Context.MODE_PRIVATE);
String myString = settings.getString("mystring", "defaultvalue");
您可以在http://developer.android.com/guide/topics/data/data-
storage.html#pref中
找到更多信息。
游戏活动中: 这也适用于:
对于下面的代码,我正在尝试检索共享的首选项,我认为它保存正确,但当我回到登录屏幕时,所有的数据都消失了。我需要它留在我回到这个屏幕上。所以我在个人资料页面上输入姓名、年龄和id到三个单独的行中。然后按下save按钮,然后按下action Bar上的back转到前面的页面。当我回到个人资料页面时,我的信息应该还在那里,但它没有任何帮助?
非常感谢您的光临,我正在为学校开发一个应用程序,我遇到了另一个问题。该应用程序的主要思想是跟踪您的卡路里,我希望它能节省卡路里,所以当应用程序关闭时,它仍然会记住他们。我已经忙了一段时间了,现在尝试使用SavedPreferences,但我总是出现错误。我希望有人能帮我。 } 我可能做了很多明显的愚蠢的事情,但我真的弄不明白。 多谢!
更新崩溃日志:
当我发送FCM消息且应用程序处于后台时,我想清除共享首选项。在方法中,我正在调用一个方法来清除它们。 我得到以下错误: 未处理的异常:MissingPluginException(在channel plugins.flatter.io/shared_首选项上找不到方法getAll的实现)
我对Kotlin和Android,以及开发一个使用共享首选项的项目还是新手。 但是,主活动根本不刷新。我是说,我都不知道怎么做。我希望我的主要活动刷新当我从登录活动返回。 到目前为止,它只在我关闭应用程序并重新打开时工作。会话可能缓存在共享首选项中。 我试图通过点击某些按钮来启动和完成活动(例如,当用户通过所有凭据时,点击登录按钮),但它没有工作。 如果你需要更多的代码来修复这个问题,请告诉我