将多个字符串传递给SharedPreferences

Sli*_*ick 2 string android 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);



                }
            };




});}}
Run Code Online (Sandbox Code Playgroud)

gor*_*orn 5

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

public static String MY_PREFS = "MY_PREFS";
private SharedPreferences mySharedPreferences;
int prefMode = Activity.MODE_PRIVATE;
Run Code Online (Sandbox Code Playgroud)

然后存储字符串值:

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

editor.commit(); // persist the values
Run Code Online (Sandbox Code Playgroud)

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

mySharedPreferences = getSharedPreferences(MY_PREFS, prefMode);
String string1 = mySharedPreferences.getString("key1", null);
String string2 = mySharedPreferences.getString("key2", null);
Run Code Online (Sandbox Code Playgroud)

这不难查找并找到例子.通过访问Android开发者中文档,您将找到有关如何在Android手机上使用数据存储的有用网站的链接.