Java 跨活动访问共享首选项
我在这个.java文件中有一个SharedReference;在底部,您可以看到我将值保存到SharedReferences GB_PREFERENCES_BENCH和GB_PREFERENCES_BENCH。如何在其他活动中使用这些值?请参见第二个代码示例,了解如何使用它Java 跨活动访问共享首选项,java,android,sharedpreferences,Java,Android,Sharedpreferences,我在这个.java文件中有一个SharedReference;在底部,您可以看到我将值保存到SharedReferences GB_PREFERENCES_BENCH和GB_PREFERENCES_BENCH。如何在其他活动中使用这些值?请参见第二个代码示例,了解如何使用它 package com.creativecoders.gymbuddy; import com.creativecoders.gymbuddy.R; import android.app.Activity; import
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中的值)
共享首选项可以在整个应用程序中访问,因此您可以从应用程序中的任何活动中读取它们 在活动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");
当我在第二个活动中调用您的第二个代码String Curls=settings.getString(“GB_PREFERENCES_Curls”,“Curls”)时,您可以在JustDanyul上找到更多信息;我尝试更改textview的值setText(String.valueOf(Curls));它实际上只是将文本更改为卷曲。不是它的价值。有什么建议吗?
SharedPreferences settings = getSharedPreferences("mysettings",
Context.MODE_PRIVATE);
String myString = settings.getString("mystring", "defaultvalue");