将多个字符串传递给SharedPreferences

时间:2012-02-20 20:30:21

标签: android string sharedpreferences

我想将三个字符串存储为我的应用的用户首选项。我已经设置了一个很好的布局,只需将字符串保存到SharedPreferences即可。我还想知道如何在下一个活动中检索这些字符串。下面是我目前的代码,如果有人能告诉我如何将这个功能添加到代码中,我将不胜感激。这是我的应用程序中的障碍,我一直试图过几天。

主要活动代码:

package com.amritayalur.mypowerschool;



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MyPowerSchoolActivity extends Activity {
Button buttonSubmit;
TextView textViewTitle;
TextView textViewDesc;
EditText editTextURL, editTextUser, editTextPass;


 String str;
 String username;
 String password;
 String url;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    textViewTitle = (TextView) findViewById(R.id.textViewTitle);
    textViewDesc = (TextView) findViewById(R.id.textViewDesc);

    editTextURL = (EditText) findViewById(R.id.editTextURL);
    editTextUser = (EditText) findViewById(R.id.editTextUser);
    editTextPass = (EditText) findViewById(R.id.editTextPass);
    //Start TextView
    textViewTitle.setText("MyPowerSchool");


    //button listener
    buttonSubmit.setOnClickListener(new View.OnClickListener() {

         @Override
            public void onClick(View v) 
            {
                if (  ( !editTextURL.getText().toString().equals("")) && (
!editTextUser.getText().toString().equals("")) && (
!editTextPass.getText().toString().equals("") ) ) 
                {

                    url = editTextURL.getText().toString();
                    username = editTextUser.getText().toString();
                    password = editTextPass.getText().toString();


                    // TODO Auto-generated method stub
                    //Intent i = new Intent( MyPowerSchoolActivity.this,
creds.class);    


                   //startActivity(i);



                }
            };




});}}

4 个答案:

答案 0 :(得分:5)

只需在要使用SharedPreferences的活动中设置这些类变量:

public static String MY_PREFS = "MY_PREFS";
private SharedPreferences mySharedPreferences;
int prefMode = Activity.MODE_PRIVATE;

然后存储字符串值:

SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("key1", "value1");
editor.putString("key2", "value2");
editor.putString("key3", "value3");

editor.commit(); // persist the values

要在另一个Activity / class中阅读它们:

mySharedPreferences = getSharedPreferences(MY_PREFS, prefMode);
String string1 = mySharedPreferences.getString("key1", null);
String string2 = mySharedPreferences.getString("key2", null);

查找并查找示例并不难。转到documentation in Android Developers,您会找到有关如何在Android手机上使用数据存储的this useful site链接。

答案 1 :(得分:2)

以某种方式定义您的首选项字段名称。通过最终的静态场来做这件事是很常见的。它可以在您的活动类中的某个单独的类中完成。给他们一些uniq名字。

public static final String MY_PARAM_1 = "com.amritayalur.mypowerschool.PARAM_1";
public static final String MY_PARAM_2 = "com.amritayalur.mypowerschool.PARAM_2";

获取共享首选项实例

// "this" is your activity or context
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 

要将字符串放在首选项中,您需要创建一个编辑器

SharedPreferences.Editor editor = prefs.edit();

然后输入你的值

editor.putString(MY_PARAM_1, "First string");
editor.putString(MY_PARAM_2, "Second string");

然后你就完成了,提交了你的更改

editor.commit();

要获取值,请使用getString方法

// specify default value in case if preferences are empty or current key is not set
prefs.getString(MY_PARAM_1, "default value");

答案 2 :(得分:0)

您可以添加多个这样的值:

preferences.edit()
      .putLong(DAYS_LEFT, premiumStatus.getDaysLeft())
      .putString(KEY, premiumStatus.getKey())
      .putString(ID, premiumStatus.getId())
      .apply();

答案 3 :(得分:0)

首先设置共享首选项。为此,请在创建之前设置此行:

  private Context mContext; 

然后把它放在oncreate:

                    mContext = this;   

然后将您的值存储在SharedPreferece:

 SharedPreferences settings = 
PreferenceManager.getDefaultSharedPreferences(mcontext); 
SharedPreferences.Editor editor = settings.edit();

url = editTextURL.getText().toString();
username = editTextUser.getText().toString();
password = editTextPass.getText().toString();

editor.putString("kye1", url);
editor.putString("kye2", username);
editor.putString("kye3", password);

然后从另一个类(或任何地方)检索您的值:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mcontext); 
 String value1=settings.getString("key1",""); // for url
 String value2=settings.getString("key2",""); // for name
 String value3=settings.getString("key3",""); // for password

然后设置这个" value1 / 2/3"任何地方。