传递多个字符串共享preferences [英] Passing multiple strings to SharedPreferences
问题描述
我要存储三个字符串作为用户preferences我的应用程序。我有一个很好的布局已经成立,它只是节省了字符串共享preferences的问题。我也想知道我可以在接下来的活动中检索这些字符串。下面是我目前的code,我将不胜AP preciate,如果有人可以告诉我我怎么能这个功能添加到code。这是我的应用程序的一个路障我一直在试图让过去几天了。
code为主要活动:
包com.amritayalur.mypowerschool;进口android.app.Activity;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.content.Shared preferences.Editor;
进口android.os.Bundle;
。进口的Android preference preferenceActivity;
。进口的Android preference preferenceManager。
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;公共类MyPowerSchoolActivity延伸活动{
按钮buttonSubmit;
TextView的textViewTitle;
TextView的textViewDesc;
的EditText editTextURL,editTextUser,editTextPass;
字符串str;
字符串用户名;
字符串密码;
字符串URL;/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
buttonSubmit =(按钮)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);
//开始的TextView
textViewTitle.setText(MyPowerSchool);
//按钮侦听
buttonSubmit.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v)
{
如果((editTextURL.getText()的toString()等于())及!。及(
!editTextUser.getText()的toString()等于())及。&放大器; (
!editTextPass.getText()。的toString()。等于()))
{ 。URL = editTextURL.getText()的toString();
。用户名= editTextUser.getText()的toString();
。密码= editTextPass.getText()的toString();
// TODO自动生成方法存根
//意图I =新意图(MyPowerSchoolActivity.this,
creds.class);
// startActivity(ⅰ); }
};
});}}
刚刚成立,这些类变量在活动中要使用共享preferences:
公共静态字符串MY_ preFS =MY_ preFS
私人共享preferences myShared preferences;
INT prefMode = Activity.MODE_PRIVATE;
然后存储字符串值:
共享preferences.Editor编辑= myShared preferences.edit();
editor.putString(KEY1,值1);
editor.putString(KEY2,值2);
editor.putString(KEY3,值3);editor.commit(); //坚持的价值观
要在另一个活动/类阅读:
myShared preferences = getShared preferences(MY_ preFS,prefMode);
字符串字符串1 = myShared preferences.getString(KEY1,NULL);
字符串字符串2 = myShared preferences.getString(KEY2,NULL);
这是不难查找和发现的例子。通过转到Android开发者的文档你会发现链接到本有用的网站上如何操作到Android手机上使用的数据存储。
I want to store three strings as user preferences for my app. I have a nice layout already set up, it's just a matter of saving the strings to the SharedPreferences. I would also like to know how I can retrieve these strings in the next activity. Below is my current code, I would greatly appreciate it if someone could show me how I can add this functionality to the code. This is a roadblock in my app I have been trying to get past for a few days now.
Code for main activity:
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);
}
};
});}}
Just set up these class variables in the Activities you want to use SharedPreferences:
public static String MY_PREFS = "MY_PREFS";
private SharedPreferences mySharedPreferences;
int prefMode = Activity.MODE_PRIVATE;
And then to store string values:
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("key1", "value1");
editor.putString("key2", "value2");
editor.putString("key3", "value3");
editor.commit(); // persist the values
To read them in another Activity/class:
mySharedPreferences = getSharedPreferences(MY_PREFS, prefMode);
String string1 = mySharedPreferences.getString("key1", null);
String string2 = mySharedPreferences.getString("key2", null);
This is not hard to look up and find examples of. By going to the documentation in Android Developers you will find a link to this useful site on how to use data storage on Android phones.
这篇关于传递多个字符串共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!