Android登录记住密码
sm2011616
10年前
private Button login_enter,login_reg; private EditText login_name,login_password; private ProgressDialog pd; private int Flag,Id; private CheckBox login_check1,login_check2; private Handler messageHandler; private String state; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); login_enter=(Button) findViewById(R.id.login_enter); login_reg=(Button) findViewById(R.id.login_reg); login_name=(EditText) findViewById(R.id.login_name); login_password=(EditText) findViewById(R.id.login_pass); login_check1=(CheckBox) findViewById(R.id.login_check1); login_check2=(CheckBox) findViewById(R.id.login_check2); //SharedPreferences将name 和 pass 记录起来 每次进去软件时 开始从中读取数据 放入login_name,login_password中 SharedPreferences remdname=getPreferences(Activity.MODE_PRIVATE); String name_str=remdname.getString("name", ""); String pass_str=remdname.getString("pass", ""); login_name.setText(name_str); login_password.setText(pass_str); login_check1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { SharedPreferences remdname=getPreferences(Activity.MODE_PRIVATE); SharedPreferences.Editor edit=remdname.edit(); edit.putString("name", login_name.getText().toString()); edit.putString("pass", login_password.getText().toString()); edit.commit(); } if(!isChecked) { SharedPreferences remdname=getPreferences(Activity.MODE_PRIVATE); SharedPreferences.Editor edit=remdname.edit(); edit.putString("name", ""); edit.putString("pass", ""); edit.commit(); } } }); //登录事件 login_enter.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //在这写登录后的事件内容 if(login_check1.isChecked())//检测用户名密码 { SharedPreferences remdname=getPreferences(Activity.MODE_PRIVATE); SharedPreferences.Editor edit=remdname.edit(); edit.putString("name", login_name.getText().toString()); edit.putString("pass", login_password.getText().toString()); edit.commit(); } } });