如何在Android中使用SharedPreferences來存儲,獲取和編輯值

已關閉 。 這個問題須要更加集中。 它當前不接受答案。 瞭解更多

想改善這個問題嗎? 更新問題,使其僅經過編輯此帖子來關注一個問題。 java

去年關閉。 android

這篇文章是社區維基 。 編輯現有答案以改善此職位。 它當前不接受新的答案。 瞭解更多

我想存儲一個時間值,須要檢索和編輯它。 如何使用SharedPreferences作到這一點? app


#1樓

要將值存儲在共享首選項中: 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*/
}

#2樓

經過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();

如今,您能夠像其餘類同樣在其餘地方使用它。 如下是OutputClassutf-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);

#3樓

editor.putString("text", mSaved.getText().toString());

在這裏, mSaved能夠是咱們能夠從中提取字符串的任何TextViewEditText 。 您只需指定一個字符串便可。 此處的文本將是保存從mSavedTextViewEditText )得到的值的鍵。 rem

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

一樣,也不須要使用程序包名稱「 com.example.app」保存首選項文件。 您能夠說起本身的首選名稱。 但願這能夠幫助 !


#4樓

來寫 :

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", "");

#5樓

在任何應用程序中,均可以經過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>
相關文章
相關標籤/搜索