跨活动访问共享首选项 [英] Access Shared Preferences Across Activities

查看:100
本文介绍了跨活动访问共享首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个.java文件中有一个SharedPreference;在底部,您可以看到我将值保存到SharedPreferences GB_PREFERENCES_BENCH和GB_PREFERENCES_FLIES。如何在其他活动中使用这些值?请参阅第二个代码示例,了解我如何使用它。

I have a SharedPreference in this .java File; towards the bottom you can see I save the values to the SharedPreferences GB_PREFERENCES_BENCH, and GB_PREFERENCES_FLIES. How do I use these values in another activity? See the second code example for how I want to use it.

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中的值)

Here is how I want to use it; (specifically in the on create method to set a TextView's text to the value in the 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);
        }
    }

}


推荐答案

共享首选项可在整个应用程序中访问,因此您可以从应用程序中的任何活动中读取它们。

The shared preferences are accessible throughout your application, so you can read them from any activity in the application.

存储键/值对在活动A:

Storing a key/value pair in activity A:

SharedPreferences settings = getSharedPreferences("mysettings", 
     Context.MODE_PRIVATE);

SharedPreferences.Editor editor = settings.edit();
editor.putString("mystring", "wahay");
editor.commit();

从另一项活动中读取此值:

Reading this value from another activity:

SharedPreferences settings = getSharedPreferences("mysettings", 
    Context.MODE_PRIVATE);
String myString = settings.getString("mystring", "defaultvalue");

您可以在 http://developer.android.com/guide/topics/data/data-storage.html#pref

这篇关于跨活动访问共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆