设为首页 加入收藏

TOP

用户登录保存数据实例(慕课笔记 使用SharedPreferences保存用户名)(二)
2017-10-12 18:10:55 】 浏览:5794
Tags:用户 登录 保存 数据 实例 慕课 笔记 使用 SharedPreferences 户名
ty extends Activity { EditText etUserName,etUserPass; CheckBox chk; SharedPreferences pref; Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定对应布局控件 etUserName=(EditText)findViewById(R.id.etuserName); etUserPass=(EditText)findViewById(R.id.etuserPass); chk=(CheckBox)findViewById(R.id.checkBox1); pref=getSharedPreferences("UserInfo",MODE_PRIVATE); editor=pref.edit(); String name=pref.getString("userName", ""); if(name==null){ chk.setChecked(false); }else{ chk.setChecked(true); etUserName.setText(name); } } //为按钮添加响应 public void doClick(View v){ switch(v.getId()){ case R.id.btnLogin: //转成字符串进行判断 String name=etUserName.getText().toString().trim(); String pass=etUserPass.getText().toString().trim(); if("admin".equals(name)&&"123456".equals(pass)){ if(chk.isChecked()){ //用户名与密码均正确且保存用户名确认框处于选, //则保存数据并提交到数据库 editor.putString("userName", name); editor.commit(); }else{ editor.remove("userName"); editor.commit(); } //加信息提示框 Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show(); }else{Toast.makeText(MainActivity.this, "禁止登录", Toast.LENGTH_LONG).show(); } break; default: break; } } }

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇使用SharePreferences存取数据(.. 下一篇Google Play发布App中遇到"..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目