我有 2 个活动 1>Register.java 2>Home.java 在 Register 活动中,我输入了名字、姓氏、员工 ID 和密码。当我按下注册按钮时,控件将转移到 Home.java 活动。在这个过程中,我想捕捉名字和姓氏的值,并在家庭活动中显示相同的值。注册活动没有任何历史记录,因为它是一个注册页面,当我在按钮上从注册导航到主页活动时,它会显示名称,但是当我按下主页或后退按钮并再次尝试查看应用程序的状态时,它不会显示名字。
这是我的代码 Register.java
Button b = (Button) findViewById(R.id.btnRegister);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.reg_employeeid);
EditText password = (EditText) findViewById(R.id.reg_password);
String welcm ="Welcome";
String space = " ";
EditText firstname = (EditText)findViewById(R.id.reg_firstname);
EditText lastname = (EditText)findViewById(R.id.reg_lastname);
String getfirstname = firstname.getText().toString();
String getlasttname = lastname.getText().toString();
String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
if(username.getText().toString().equals("test") && password.getText().toString().equals("test")) {
SharedPreferences settings = `enter getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("logged", "logged");
editor.commit();
Intent intent = new Intent(Register.this, SecureXActivity.class);
intent.putExtra("name", welcome);
startActivity(intent);
}
}
}
});
主页.java
public void onCreate(Bundle savedInstanceState)
{
Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras =getIntent().getExtras();
if (extras != null)
{
String value = extras.getString("name");
TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm);
txtwelcm.setText(value);
}
}