对于下面的代码,我试图检索共享的首选项,我认为它已正确保存,但是当我返回登录屏幕时,所有数据都消失了。回到该屏幕时,我需要保留它。 因此,我在个人资料页面的三行中分别输入了姓名,年龄和ID。 然后我按保存按钮 然后再按一下操作栏上的,转到上一页。 当我返回个人资料页面时,我的信息仍应存在,但不存在 有帮助吗?
package com.example.myprofile;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Savepoint;
public class Profile extends AppCompatActivity {
protected EditText NameEditText;
protected EditText AgeEditText;
protected EditText IDEditText;
protected Button saveButton;
protected Button settings_id;
String name;
String age;
String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
EditText mEdit = (EditText) findViewById(R.id.NameEditText);
mEdit.setEnabled(false);
EditText mEdit1 = (EditText) findViewById(R.id.AgeEditText);
mEdit1.setEnabled(false);
EditText mEdit2 = (EditText) findViewById(R.id.IDEditText);
mEdit2.setEnabled(false);
NameEditText = (EditText) findViewById(R.id.NameEditText);
AgeEditText = (EditText) findViewById(R.id.AgeEditText);
IDEditText = (EditText) findViewById(R.id.IDEditText);
settings_id = (Button) findViewById(R.id.settings_id);
saveButton = (Button) findViewById(R.id.SaveButton);
SharedPreferences prefs = getSharedPreferences(getString(R.string.ProfileName), Context.MODE_PRIVATE);
name = prefs.getString("userName", "");
age = prefs.getString("userAge", "");
id = prefs.getString("userID", "");
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = NameEditText.getText().toString();
String age = AgeEditText.getText().toString();
String id = IDEditText.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.ProfileName), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.ProfileName), name);
editor.putString(getString(R.string.ProfileAge), age);
editor.putString(getString(R.string.ProfileID), id);
editor.apply();
if (Integer.parseInt(age) < 18)
{
Toast toast1 = Toast.makeText(getApplicationContext(), "Invalid Age", Toast.LENGTH_LONG);
toast1.show();
}
else if (!name.isEmpty() && !age.isEmpty() && !id.isEmpty())
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Toast toast = Toast.makeText(getApplicationContext(), "Name Saved!", Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast2 = Toast.makeText(getApplicationContext(), "Incomplete Info", Toast.LENGTH_LONG);
toast2.show();
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings_id:
{
EditText mEdit = (EditText) findViewById(R.id.NameEditText);
mEdit.setEnabled(true);
EditText mEdit1 = (EditText) findViewById(R.id.AgeEditText);
mEdit1.setEnabled(true);
EditText mEdit2 = (EditText) findViewById(R.id.IDEditText);
mEdit2.setEnabled(true);
saveButton.setEnabled(Boolean.parseBoolean("True"));
}
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
答案 0 :(得分:1)
要保存数据,请使用以下代码示例
name = NameEditText.getText().toString();
age = AgeEditText.getText().toString();
id = IDEditText.getText().toString();
SharedPreferences prefs = getSharedPreferences(
"com.example.myprofile", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("userName", name);
editor.putString("userAge", age);
editor.putString("userID", id);
editor.apply();
要获取数据,请使用以下代码示例
SharedPreferences prefs = getSharedPreferences(
"com.example.myprofile", Context.MODE_PRIVATE);
name = prefs.getString("userName", "");
age = prefs.getString("userAge", "");
id = prefs.getString("userID", "");
在onCreate
方法之前
String name;
String age;
String id;
说明:
getSharedPreferences
的第一个参数是您的程序包名称,基本上是代码的第一行。SharedPreferences
实例,一个就足够了SharedPreferences.Editor
实例,一个就足够了。key
(例如用户的用户名)来保存数据,因为那样一来,您就需要通过意图将密钥传递给其他活动,并且,如果要这样做,为什么不发送用户名而不是密钥? editor.apply()
代替editor.commit()
onPause()
中并在onResume()
中进行检索,因此将它们全局声明是有用的,以避免编写额外的代码行。答案 1 :(得分:0)
尝试(在行尾使用apply()):
editor.putString(getString(R.string.ProfileName), name).apply();
editor1.putString(getString(R.string.ProfileAge), age).apply();
editor2.putString(getString(R.string.ProfileID), id).apply();
答案 2 :(得分:0)
您正在使用特定的字符串(getString(R.string.ProfileAge)....)通过commit()或apply()在私有模式下以共享首选项保存数据:-< / p>
SharedPreferences sharedPreferences1 = getSharedPreferences(getString(R.string.ProfileAge), Context.MODE_PRIVATE);
SharedPreferences sharedPreferences2 = getSharedPreferences(getString(R.string.ProfileID), Context.MODE_PRIVATE);
但是您不尝试在私有模式下使用这些字符串获取数据,您必须使用:-
SharedPreferences sharedPref = getSharedPreferences(getString(R.string.ProfileAge), Context.MODE_PRIVATE);
String name = sharedPref.getString("key", "defaultValue");
答案 3 :(得分:0)
要保存
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("keyname",true);
editor.putString("keyname","string value");
editor.putInt("keyname","int value");
editor.putFloat("keyname","float value");
editor.putLong("keyname","long value");
editor.commit();
要获得
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
pref.getString("keyname",null);
pref.getInt("keyname",0);
pref.getFloat("keyname",0);
pref.getBoolean("keyname",true);
pref.getLong("keyname",0);
对于单个删除
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.remove("keyname");
editor.commit();
对于所有删除
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
答案 4 :(得分:0)
您可以使用共享首选项
要优先保存数据
SharedPreferences share = getSharedPreferences("Data", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = share.edit();
edit.putLong("user_id", 1);
edit.putString("token","1243434sfdfsf");
edit.commit();
要从偏好中获取数据
SharedPreferences share = getSharedPreferences("Data", Context.MODE_PRIVATE);
share.getLong("user_id", 0);
share.getString("token", "");
您可以尝试以下链接以获取更多详细信息:Shared preference in android