如何使用 sharedPrefrence 的 putString 方法?

huangapple go评论68阅读模式
英文:

How to use sharedPrefrence putString?

问题

我想在另一个活动中使用 sharedprefrence 字符串形式,但值没有被传递,它总是传递默认值?
变量 toast 的创建显示了输入值,但值没有被传递

nameInput = (EditText) findViewById(R.id.nameInput);
name_next = (Button) findViewById(R.id.name_next);
sp = getSharedPreferences("name_pref", Context.MODE_PRIVATE);

name_next.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        inp = nameInput.getText().toString();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("inp", inp);
        editor.commit();
        Toast.makeText(com.calmo.name.this, "welcome " + inp, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(name.this, MainActivity.class);
        startActivity(intent);
    }
});    

调用
该值始终返回默认值错误

SharedPreferences sp = getApplicationContext().getSharedPreferences("name_pref", Context.MODE_PRIVATE);
String name_in = sp.getString("inp", "Error");
name.setText(name_in);
英文:

I want to use sharedprefrence string form another activity but the value is not being passed it always passes the default value?
Creation of variable toast Shows inp value but value is not being passed

                name_next = (Button) findViewById(R.id.name_next);
                sp= getSharedPreferences("name_pref",Context.MODE_PRIVATE);

                name_next.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        inp = nameInput.getText().toString();
                        SharedPreferences.Editor editor= sp.edit();
                        editor.putString(inp,nameInput);
                        editor.commit();
                        Toast.makeText(com.calmo.name.this,"welcome "+inp,Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(name.this,MainActivity.class);
                        startActivity(intent);
                    }
                });```    
**Calling **
**The Value is always returned as error that is default**

```            SharedPreferences sp=getApplicationContext().getSharedPreferences("name_pref",Context.MODE_PRIVATE);
        String name_in =sp.getString("inp","Error");
        name.setText(name_in);


</details>


# 答案1
**得分**: 0

移除点击事件中的
`inp = nameInput.getText().toString();`
并将
`editor.putString(inp,nameInput);`
**更改为**
`editor.putString(&quot;inp&quot;,nameInput.getText().toString());`

<details>
<summary>英文:</summary>

Remove
`inp = nameInput.getText().toString();`
under onclick
and change 
`editor.putString(inp,nameInput);`
**to**
`editor.putString(&quot;inp&quot;,nameInput.getText().toString());`

</details>



# 答案2
**得分**: 0

```java
public class AppSharedPreferences {
    public static final String TAG = "AppSharedPreferences";
    private static SharedPreferences sharedPref;

    private String demo_string = "demo string's key";

    public AppSharedPreferences() {

    }

    public static void init(Context context) {
        if (sharedPref == null)
            sharedPref = context.getSharedPreferences(
                context.getPackageName(), Activity.MODE_PRIVATE);
    }

    public static void setDemoString(String demoString) {
        Editor prefs = sharedPref.edit();
        prefs.putString(demo_string, demoString);
        prefs.apply();
    }

    public static String getDemoString() {
        return sharedPref.getString(demo_string, "");
    }
}

将此部分放在您想要使用 shared prefs 的位置:

AppSharedPreferences.init(this);

根据您在代码中使用的位置,可以将 this 替换为 activitycontext

要在首选项中设置值,请使用以下代码:

AppSharedPreferences.setDemoString("Some Text");

要从首选项中获取值:

String text = AppSharedPreferences.getDemoString();
英文:
public class AppSharedPreferences {
public static final String TAG = &quot;AppSharedPreferences&quot;;
private static SharedPreferences sharedPref;

private String demo_string = &quot;demo string&#39;s key&quot;

public AppSharedPreferences() {

}

public static void init(Context context)
{
    if(sharedPref == null)
        sharedPref = context.getSharedPreferences(
            context.getPackageName(), Activity.MODE_PRIVATE);
}

public static void setDemoString(String demoString) {
    Editor prefs = sharedPref.edit();
    prefs.putString(demo_string, demoString);
    prefs.apply();
}

public static String getDemoString() {
    return sharedPref.getString(demo_string, &quot;&quot;);
}}

Put this wherever u want to use shared prefs:

AppSharedPreferences.init(this);

You can exchange this for activity or context, depending on where you use the code.

To set value in preference, use this code:

AppSharedPreferences.setDemoString(&quot;Some Text&quot;);

To get the value from preference:

String text = AppSharedPreferences.getDemoString();

huangapple
  • 本文由 发表于 2020年9月29日 21:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64120419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定