如何使用android中的共享首选项将数据保存在editText中[重复]
Posted
技术标签:
【中文标题】如何使用android中的共享首选项将数据保存在editText中[重复]【英文标题】:how to save data in editText using Shared Preferences in android [duplicate] 【发布时间】:2016-10-09 14:17:33 【问题描述】:我有两个编辑文本,我想将数据保存在编辑文本中,直到我没有更改它的制作方法。我尝试这样的事情,但数据不保存。
e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String homewifi = e1.getText().toString();
String officewifi = e2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
editor.putString(OFFICEWIFI, officewifi);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();
);
当活动再次打开时,数据不会显示在edittext中。
【问题讨论】:
您正在保存数据但不检索数据 @Masoom Bacha 在下面看到我的回答。 【参考方案1】:您没有从 sharedpreferences 中检索数据,请修改如下代码:
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
String homewifi = sharedpreferences.getString(HOMEWIFI, "");
String officeWifi = sharedpreferences.getString(OFFICEWIFI, "");
e1.setText(homewifi);
e2.setText(officeWifi);
b1 = (Button)findViewById(R.id.button3);
Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String homewifi = e1.getText().toString();
String officewifi = e2.getText().toString();
editor.putString(HOMEWIFI, homewifi);
editor.putString(OFFICEWIFI, officewifi);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();
);
【讨论】:
看这里我有点小问题 @MasoomBacha 是的,告诉我这是什么? if(con.getSSID().toString().equalsIgnoreCase("\"Ranksol\"")) 我在这里使用手动字符串值,它工作正常 我想使用从edittext获得的字符串值 String homewifi = e1.getText().toString();【参考方案2】:当活动再次打开时,EditText 中不显示数据。
需要从SharedPreferences
获取数据并调用EditText's
的setText
方法在启动Activity
Again.like时显示:
....
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(HOMEWIFI))
String strHomeWifi = settings.getString(HOMEWIFI, "default value");
e1.setText(strHomeWifi);
else if (sharedpreferences.contains(OFFICEWIFI))
String strofficewifi = settings.getString(OFFICEWIFI, "default value");
e2.setText(strofficewifi);
...
【讨论】:
我应该把这段代码放在哪里? @MasoomBacha:不检查我的答案。在代码中的b1.setOnClickListener(new View.OnClickListener()
行之前执行此操作
e1.setText(strHomeWifi);这里的str是什么
@MasoomBacha:整个代码是String strHomeWifi = settings.getString(HOMEWIFI, "default value"); e1.setText(strHomeWifi);
@MasoomBacha:那一行有什么问题吗?【参考方案3】:
我猜你忘记将 SharedPreferences 中的字符串设置为两个编辑文本,
在将其从编辑文本保存到共享首选项之后。
喜欢
e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if(!TextUtils.isEmpty(sharedpreferences.getString(HOMEWIFI)))
e1.setText(sharedpreferences.getString(HOMEWIFI));
if(!TextUtils.isEmpty(sharedpreferences.getString(OFFICEWIFI)))
e1.setText(sharedpreferences.getString(OFFICEWIFI));
b1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String homewifi = e1.getText().toString();
String officewifi = e2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
editor.putString(OFFICEWIFI, officewifi);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();
);
【讨论】:
以上是关于如何使用android中的共享首选项将数据保存在editText中[重复]的主要内容,如果未能解决你的问题,请参考以下文章
Android - 保存自定义对象 - 共享首选项或数据库?