想改善这个问题吗? 更新问题,使其仅经过编辑此帖子来关注一个问题。 java
去年关闭。 android
我想存储一个时间值,须要检索和编辑它。 如何使用SharedPreferences
作到这一点? app
要将值存储在共享首选项中: ide
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = preferences.edit(); editor.putString("Name","Harneet"); editor.apply();
要从共享的首选项中检索值: this
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); String name = preferences.getString("Name", ""); if(!name.equalsIgnoreCase("")) { name = name + " Sethi"; /* Edit the value here*/ }
经过SharedPreferences
如何存储登陆值的简单解决方案。 spa
您能够扩展MainActivity
类或其余将存储“您想要保留的东西的值”的类。 将其放入做家和读者类中: code
public static final String GAME_PREFERENCES_LOGIN = "Login";
在这里, InputClass
是输入类,而OutputClass
是输出类。 xml
// This is a storage, put this in a class which you can extend or in both classes: //(input and output) public static final String GAME_PREFERENCES_LOGIN = "Login"; // String from the text input (can be from anywhere) String login = inputLogin.getText().toString(); // then to add a value in InputCalss "SAVE", SharedPreferences example = getSharedPreferences(GAME_PREFERENCES_LOGIN, 0); Editor editor = example.edit(); editor.putString("value", login); editor.commit();
如今,您能够像其余类同样在其余地方使用它。 如下是OutputClass
。 utf-8
SharedPreferences example = getSharedPreferences(GAME_PREFERENCES_LOGIN, 0); String userString = example.getString("value", "defValue"); // the following will print it out in console Logger.getLogger("Name of a OutputClass".class.getName()).log(Level.INFO, userString);
editor.putString("text", mSaved.getText().toString());
在这里, mSaved
能够是咱们能够从中提取字符串的任何TextView
或EditText
。 您只需指定一个字符串便可。 此处的文本将是保存从mSaved
( TextView
或EditText
)得到的值的键。 rem
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
一样,也不须要使用程序包名称“ com.example.app”保存首选项文件。 您能够说起本身的首选名称。 但愿这能够帮助 !
来写 :
SharedPreferences preferences = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_WORLD_WRITEABLE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("Authentication_Id",userid.getText().toString()); editor.putString("Authentication_Password",password.getText().toString()); editor.putString("Authentication_Status","true"); editor.apply();
读书 :
SharedPreferences prfs = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_PRIVATE); String Astatus = prfs.getString("Authentication_Status", "");
在任何应用程序中,均可以经过PreferenceManager
实例及其相关方法getDefaultSharedPreferences(Context)
来访问默认首PreferenceManager
。
使用SharedPreference
实例,可使用getInt(String key,int defVal)检索任何首选项的int值。 在这种状况下,咱们感兴趣的偏好是对立的。
在咱们的案例中,咱们可使用edit()修改咱们的案例中的SharedPreference
实例,并使用putInt(String key, int newVal)
咱们增长了存在于应用程序以外并所以显示的应用程序计数。
为了进一步演示,请从新启动并从新启动应用程序,您会注意到,每次从新启动应用程序时,计数都会增长。
PreferencesDemo.java
码:
package org.example.preferences; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.widget.TextView; public class PreferencesDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Get the app's shared preferences SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this); // Get the value for the run counter int counter = app_preferences.getInt("counter", 0); // Update the TextView TextView text = (TextView) findViewById(R.id.text); text.setText("This app has been started " + counter + " times."); // Increment the counter SharedPreferences.Editor editor = app_preferences.edit(); editor.putInt("counter", ++counter); editor.commit(); // Very important } }
main.xml
码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>