对于下面的代码,我正在尝试检索共享首选项,我认为它保存正确,但是当我返回登录屏幕时,所有数据都消失了。当我回到这个屏幕时,我需要它保留。所以我将姓名、年龄和身份证输入到个人资料页面上的三个单独的行中。然后我按下保存按钮然后通过按下操作栏返回之前的页面。当我回到个人资料页面时,我的信息应该仍然存在,但它没有任何帮助吗?
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", "");
尚方宝剑之说
MMTTMM
慕森王
相关分类