我有一个允许某人注册帐户的应用程序,在其个人资料的第一页上,我将数据从另一个活动的一些edittexts发送到一些textviews。现在,我需要的信息是注销后保留在那里的信息(名字,姓氏和年龄)。当用户再次登录时,他应该能够在其帐户的第一页上看到该信息。我怎样才能做到这一点?谢谢。
这是代码:

public class AccountInformationSetter extends AppCompatActivity {
    EditText etFirstName, etLastName, etAge;
    Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_information_setter);

        etFirstName = (EditText) findViewById(R.id.etFirstName);
        etLastName = (EditText) findViewById(R.id.etLastName);
        etAge = (EditText) findViewById(R.id.etAge);
        btnSave = (Button) findViewById(R.id.btnSaveInfo);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String first = etFirstName.getText().toString();
                final String last = etLastName.getText().toString();
                final String age = etAge.getText().toString();

                Intent intent = new Intent(AccountInformationSetter.this, Profile.class);
                intent.putExtra("firstName", first);
                intent.putExtra("lastName", last);
                intent.putExtra("age", age);
                startActivity(intent);
            }
        });
    }


这是活动首页的代码:

public class Profile extends AppCompatActivity {
    TextView tv, tvFirstName, tvLastName, tvAge ;
    UserSessionManager session;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        Intent in = getIntent();
        String tv1 = in.getExtras().getString("name");
        tv = (TextView) findViewById(R.id.textView);
        tv.setText(tv1);

        Intent intent = getIntent();
        String first = in.getExtras().getString("firstName");
        tvFirstName = (TextView) findViewById(R.id.tvFirstName);
        tvFirstName.setText("First name: " + first);

        Intent intent1 = getIntent();
        String last = in.getExtras().getString("lastName");
        tvLastName = (TextView) findViewById(R.id.tvLastName);
        tvLastName.setText("Last name: "+ last);

        Intent intent2 = getIntent();
        String age = in.getExtras().getString("age");
        tvAge = (TextView) findViewById(R.id.tvAge);
        tvAge.setText("Age: " + age);


    }

最佳答案

使用SharedPreferences来存储特定事件的输入值,然后再次登录时检查是否存在共享状态,如果是,则从SharedPreferences中检索数据并填充到editText

07-24 09:37