英文:
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("inp",nameInput.getText().toString());`
<details>
<summary>英文:</summary>
Remove
`inp = nameInput.getText().toString();`
under onclick
and change
`editor.putString(inp,nameInput);`
**to**
`editor.putString("inp",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
替换为 activity
或 context
。
要在首选项中设置值,请使用以下代码:
AppSharedPreferences.setDemoString("Some Text");
要从首选项中获取值:
String text = AppSharedPreferences.getDemoString();
英文:
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, "");
}}
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("Some Text");
To get the value from preference:
String text = AppSharedPreferences.getDemoString();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论