我使用SharedPreferences来保存登录数据,就像记住我一样,
我设置了三个按钮,它有保存,清除,读取功能,
但在我保存一个登录数据后,我无法再更新保存信息,
顺便说一句,我已经设置了一个复选框来保存数据
但它看起来也不起作用..
有人可以帮我解决这个问题吗?
这是我的代码:
package com.example.escanui;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
public class UserInfoActivity extends ActionBarActivity {
public MultiAutoCompleteTextView userName;
public MultiAutoCompleteTextView ipAddress;
public Button loginConfirmBtw;
public EditText passWord;
public CheckBox memUserInfoCb;
public Button save, read, clear;
private SharedPreferences settings;
private static final String data = "DATA";
public static final String nameField ="NAME";
public static final String passwordField = "PASSWORD";
public static final String ipField = "IP";
public static final String filePathFile ="FILEPATH";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info);
ipAddress = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
userName = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView2);
passWord = (EditText) findViewById(R.id.editPassword1);
loginConfirmBtw = (Button) findViewById(R.id.loginButton);
memUserInfoCb = (CheckBox) findViewById(R.id.checkBox1);
save = (Button) findViewById(R.id.saveBtw);
read = (Button) findViewById(R.id.readBtw);
clear = (Button) findViewById(R.id.clearBtw);
setEventListener();
if (memUserInfoCb.isChecked())
{
saveUserInfo();
readData();
}else{
ipAddress.setText("");
userName.setText("");
passWord.setText("");
}
}
public void readData()
{
settings = getSharedPreferences(data,0);
userName.setText(settings.getString(nameField, ""));
ipAddress.setText(settings.getString(ipField, ""));
passWord.setText(settings.getString(passwordField, ""));
}
public void saveUserInfo()
{
settings = getSharedPreferences(data, 0);
settings.edit().putString(ipField, ipAddress.getText().toString());
settings.edit().putString(nameField, userName.getText().toString());
settings.edit().putString(passwordField, passWord.getText().toString());
settings.edit().commit();
}
public void setEventListener(){
save.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
saveUserInfo();
}
});
read.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
readData();
}
});
clear.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
ipAddress.setText("");
userName.setText("");
passWord.setText("");
}
});
}
}
答案 0 :(得分:1)
您的问题是在保存数据时
每次调用settings.edit()
时,您都会创建一个新的Editor
对象。
当您致电editor.commit()
或editor.apply()
时,您会写下您对编辑器对象所做的所有更改。
因此,在您的代码中,每次调用Editor
时都会创建4个settings.edit()
个对象
settings.edit().putString(ipField, ipAddress.getText().toString());
settings.edit().putString(nameField, userName.getText().toString());
settings.edit().putString(passwordField, passWord.getText().toString());
settings.edit().commit();
只提交了最后一个settings.edit()
,但您没有对该Editor
个对象进行任何更改,因此,您什么也没有提交。
考虑一下
public void saveUserInfo()
{
settings = getSharedPreferences(data, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(ipField, ipAddress.getText().toString());
editor.putString(nameField, userName.getText().toString());
editor.putString(passwordField, passWord.getText().toString());
editor.commit();
}
答案 1 :(得分:0)
如果要更新现有数据,则应使用edit().apply()
而不是commit()
,因为它会立即覆盖值。来自documentation:
与commit()不同,后者将其首选项写入持久性 同步存储,apply()将其更改提交到内存中 SharedPreferences立即