模仿登陆,记住密码
2015-06-08 09:36
861 查看
实验效果图
退出后重新进入项目效果图
开发步骤
1.建立UI界面
2.美化布局界面
3.SharedPreferences 用来保存数据
3.1 获取.SharedPreferences对象
3.2 存入数据
3.3 判断账号密码是否正确
3.4记住账号密码
3.5 界面跳转
3.6 重新登录时 读取数据
退出后重新进入项目效果图
开发步骤
1.建立UI界面
<TextView android:id="@+id/tvUsername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/tvName" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/etUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvUsername" android:layout_below="@+id/tvUsername" android:background="@android:drawable/edit_text" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/tvPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etUsername" android:layout_below="@+id/etUsername" android:text="@string/tvPassword" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvPassword" android:layout_below="@+id/tvPassword" android:layout_marginTop="16dp" android:background="@android:drawable/edit_text" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/etPassword" android:layout_below="@+id/etPassword" android:layout_marginTop="20dp" android:background="#FF72CAE1" android:onClick="save" android:text="@string/btnLogin" /> <CheckBox android:id="@+id/cbKeeppsd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etPassword" android:layout_alignTop="@+id/btnLogin" android:layout_marginLeft="2dp" android:textSize="22dp" android:checked="true" android:text="@string/keeppsd" />
<TextView android:id="@+id/tvRegist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="21dp" android:layout_marginTop="18dp" android:text="@string/tvRegister" android:autoLink="all" android:textColorLink="#FF0066CC" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="24dp" android:src="@drawable/panda" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="28dp" android:src="@drawable/icon" />
2.美化布局界面
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:startColor="#FFACDAE5" android:endColor="#FF72CAE1" android:angle="45" /> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#55FFFFFF"/> <corners android:radius="10dp"/> </shape>
3.SharedPreferences 用来保存数据
3.1 获取.SharedPreferences对象
preferences = getSharedPreferences("data", MODE_PRIVATE);
3.2 存入数据
cbKeeppsd.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (cbKeeppsd.isChecked()) { preferences.edit().putBoolean("rem_pwd", true).commit(); } else { preferences.edit().putBoolean("rem_pwd", false).commit(); } } });
3.3 判断账号密码是否正确
if (userName.equals("zhangsan") && password.equals("123")) { Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
3.4记住账号密码
if (cbKeeppsd.isChecked()) { Editor editor = preferences.edit(); editor.putString("USER_NAME", userName); editor.putString("PASSWORD", password); editor.commit(); }
3.5 界面跳转
Intent intent = new Intent(MainActivity.this,SecondActivity.class); intent.putExtra("name", userName); startActivity(intent);
3.6 重新登录时 读取数据
public void read() { if (preferences.getBoolean("rem_pwd", false)) { cbKeeppsd.setChecked(true); etUserName.setText(preferences.getString("USER_NAME", "")); etPassword.setText(preferences.getString("PASSWORD", "")); } }
相关文章推荐
- POJ 1003
- Android 解决系统大号字体导致布局显示错乱问题
- MySQL 数据库 数据表 编辑
- win7系统管理员被禁用了怎么办
- Java对象及内存管理
- 详细整理:UITableView优化技巧
- 新工作
- Eclipse for Mac 常用快捷键
- intellij开发安卓与genymotion配合
- qt屏蔽关闭窗口事件
- cocos-js项目结构
- 测试博文发布
- 测试博文发
- JavaScript面试问题:事件委托和this
- 有趣的JavaScript原生数组函数
- 通过<frameset>和<iframe>看JavaScript中window对象parent、self、top的区别
- javascript 获取文档/屏幕的Width||Height
- 详细整理:UITableView优化技巧
- Android数据库专家秘籍(七)经验LitePal查询艺术
- 【cube】SSAS(分析服务)优化手册