protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1. 获得SharedPreferences对象, 参数一表示存储的文件, 参数二 存储文件的操作模式
sharedPreferences = getSharedPreferences("login", MODE_PRIVATE) ;
//初始化控件
userName = (EditText) findViewById(R.id.userName) ;
password = (EditText) findViewById(R.id.password) ;
btnLogin = (Button) findViewById(R.id.login) ;
//4.获取SharedPreferences保存文件中的值,取出进行使用
userName.setText(sharedPreferences.getString("name", ""));
password.setText(sharedPreferences.getString("pass", ""));
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//2. 获取Editor对象
SharedPreferences.Editor editor = sharedPreferences.edit() ;
String user = userName.getText().toString() ;
String pass = password.getText().toString() ;
//3. 将EditText中的文本内容取出,调用commit提交数据
editor.putString("user", user) ;
editor.putString("pass", pass) ;
editor.commit() ;
//Intent设置跳转到第二个Activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class) ;
//Intent传递用户输入内容
intent.putExtra("name", user) ;
intent.putExtra("pass", pass) ;
startActivity(intent);
}
});
}
}
createBundle savedInstanceState) { super.onCreate(savedInstanceState);
最新推荐文章于 2022-10-24 20:19:12 发布