实践--使用SharedPreferences实现记住密码的功能
2016-03-27 19:50
429 查看
代码如下
效果图如下
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button button_login; private CheckBox checkBox; private EditText editText_account,editText_password; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); setOnListener(); SharedPreferences sharedPreferences=getSharedPreferences("mylogin",MODE_PRIVATE); boolean isChecked=sharedPreferences.getBoolean("isChecked",false); if (isChecked){ checkBox.setChecked(isChecked); String account=sharedPreferences.getString("account",""); String password=sharedPreferences.getString("password",""); editText_account.setText(account); editText_password.setText(password); } } private void setOnListener() { button_login.setOnClickListener(this); } private void init() { button_login= (Button) findViewById(R.id.button_login); checkBox= (CheckBox) findViewById(R.id.checkBox); editText_account= (EditText) findViewById(R.id.editText_account); editText_password= (EditText) findViewById(R.id.editText_password); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button_login: String account=editText_account.getText().toString(); String password=editText_password.getText().toString(); boolean isChecked=checkBox.isChecked(); if (TextUtils.isEmpty(account)){ Toast.makeText(MainActivity.this, "账号不能为空!!!", Toast.LENGTH_SHORT).show(); }else if(TextUtils.isEmpty(password)){ Toast.makeText(MainActivity.this, "密码不能为空!!!", Toast.LENGTH_SHORT).show(); }else{ SharedPreferences.Editor editor= (SharedPreferences.Editor) getSharedPreferences("mylogin",MODE_PRIVATE).edit(); editor.putString("account",account); editor.putString("password",password); editor.putBoolean("isChecked",isChecked); editor.commit(); Toast.makeText(MainActivity.this, "存储成功!!!", Toast.LENGTH_SHORT).show(); } break; } } }
效果图如下
相关文章推荐
- CSS代码重构与优化之路
- javascript 事件处理
- WCFExpetionFaultException方法内部出现没有处理的异常
- JavaScript作用域和变量提升
- jquery -ajax
- css中可继承属性与不可继承属性
- js 匿名函数和闭包函数(js练习)
- 深入浅出nodejs学习笔记——nodejs简介
- 关于jQuery绑定事件会叠加的解决和心得总结
- 【前端学习】【jQuery选择器】
- 【前端学习】【CSS选择器】
- 用node.js对一个英语句子分析页面进行一个小爬虫
- 浅谈CSS
- selenium+phantomJS爬虫,适用于登陆限制强,点触验证码等一些场景
- 【09类和模块】——3:javascript中java式的类继承
- JavaScript-导航栏点击变色处理
- <div+css页面布局课堂笔记>8---页面浮动设计
- JSTL fn函数中字符串拼接
- 初识JavaScript,Ajax,jQuery,并比较三者关系
- Disruptor:为什么会这么快?(一)Ringbuffer的特别之处