Android——P235
实现登陆界面记住密码功能:
xml代码:
1 28 15 23 24 90 10229 30 4536 37 44 49 50 55 56 7262 63 71 76 77 81 8988
——————————————————————————————————————————
Java代码:
Button btn; EditText eduse; EditText edpsw; CheckBox checkbox; SharedPreferences sp = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn_login); eduse = (EditText) findViewById(R.id.et_number); edpsw = (EditText) findViewById(R.id.et_password); checkbox = (CheckBox) findViewById(R.id.chkRemember); sp = getSharedPreferences("userinfo",MODE_PRIVATE); if (sp.getBoolean("checkboxBoolean", true)) { eduse.setText(sp.getString("num", null)); edpsw.setText(sp.getString("psw", null)); checkbox.setChecked(true); } btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean CheckBoxLogin = checkbox.isChecked(); if (CheckBoxLogin) { SharedPreferences.Editor editor = sp.edit(); editor.putString("num", eduse.getText().toString()); editor.putString("psw", edpsw.getText().toString()); editor.putBoolean("auto", true); editor.commit(); } else { SharedPreferences.Editor editor = sp.edit(); editor.putString("num", null); editor.putString("psw", null); editor.putBoolean("auto", false); editor.commit(); } Toast.makeText(MainActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show(); } } ); }