记住密码
2015-07-04 15:10
288 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/nimaba7474/article/details/46755437
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <ImageButton
- android:id="@+id/img_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:background="@drawable/quit"/>
- <TextView
- android:id="@+id/tv_zh"
- android:layout_width="wrap_content"
- android:layout_height="35dip"
- android:layout_marginLeft="12dip"
- android:layout_marginTop="10dip"
- android:gravity="bottom"
- android:text="帐号:"
- android:textColor="#000000"
- android:textSize="18sp" />
- <EditText
- android:id="@+id/et_zh"
- android:layout_width="fill_parent"
- android:layout_height="40dip"
- android:layout_below="@id/tv_zh"
- android:layout_marginLeft="12dip"
- android:layout_marginRight="10dip" />
- <TextView
- android:id="@+id/tv_mima"
- android:layout_width="wrap_content"
- android:layout_height="35dip"
- android:layout_below="@id/et_zh"
- android:layout_marginLeft="12dip"
- android:layout_marginTop="10dip"
- android:gravity="bottom"
- android:text="密码:"
- android:textColor="#000000"
- android:textSize="18sp" />
- <EditText
- android:id="@+id/et_mima"
- android:layout_width="fill_parent"
- android:layout_height="40dip"
- android:layout_below="@id/tv_mima"
- android:layout_marginLeft="12dip"
- android:layout_marginRight="10dip"
- android:maxLines="200"
- android:password="true"
- android:scrollHorizontally="true" />
- <CheckBox
- android:id="@+id/cb_mima"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/et_mima"
- android:layout_marginLeft="12dip"
- android:text="记住密码"
- android:textColor="#000000" />
- <CheckBox
- android:id="@+id/cb_auto"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/cb_mima"
- android:layout_marginLeft="12dip"
- android:text="自动登录"
- android:textColor="#000000" />
- <Button
- android:id="@+id/btn_login"
- android:layout_width="80dip"
- android:layout_height="40dip"
- android:layout_below="@id/et_mima"
- android:layout_alignParentRight="true"
- android:layout_alignTop="@id/cb_auto"
- android:layout_marginRight="10dip"
- android:gravity="center"
- android:text="登录"
- android:textColor="#000000"
- android:textSize="18sp"/>
- </RelativeLayout>
2.主要代码
[html] view plaincopy- //判断记住密码多选框的状态
- if(sp.getBoolean("ISCHECK", false))
- {
- //设置默认是记录密码状态
- rem_pw.setChecked(true);
- userName.setText(sp.getString("USER_NAME", ""));
- password.setText(sp.getString("PASSWORD", ""));
- //判断自动登陆多选框状态
- if(sp.getBoolean("AUTO_ISCHECK", false))
- {
- //设置默认是自动登录状态
- auto_login.setChecked(true);
- //跳转界面
- Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
- LoginActivity.this.startActivity(intent);
- }
- }
- // 登录监听事件 现在默认为用户名为:admin 密码:123456
- btn_login.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- userNameValue = userName.getText().toString();
- passwordValue = password.getText().toString();
- if(userNameValue.equals("admin")&&passwordValue.equals("123456"))
- {
- Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
- //登录成功和记住密码框为选中状态才保存用户信息
- if(rem_pw.isChecked())
- {
- //记住用户名、密码、
- Editor editor = sp.edit();
- editor.putString("USER_NAME", userNameValue);
- editor.putString("PASSWORD",passwordValue);
- editor.commit();
- }
- //跳转界面
- Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
- LoginActivity.this.startActivity(intent);
- //finish();
- }else{
- Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
- }
- }
- });
- //监听记住密码多选框按钮事件
- rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- if (rem_pw.isChecked()) {
- System.out.println("记住密码已选中");
- sp.edit().putBoolean("ISCHECK", true).commit();
- }else {
- System.out.println("记住密码没有选中");
- sp.edit().putBoolean("ISCHECK", false).commit();
- }
- }
- });
- //监听自动登录多选框事件
- auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- if (auto_login.isChecked()) {
- System.out.println("自动登录已选中");
- sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
- } else {
- System.out.println("自动登录没有选中");
- sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
- }
- }
- });
- btnQuit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- }
- }
相关文章推荐
- html运用(四) html解决浏览器记住密码输入框的问题
- IOS开发之记住账号密码_NSUserDefaults
- 安卓SharedPrefences详解以及记住密码的小例子
- 删除SVN记住的密码
- 使用SharedPreferences实现登录时记住密码的功能
- JavaWeb登录、注销、退出、记住用户名和密码
- jsp login登录页面的记住密码 cookie控制
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
- 自动登录SharedPreferences记住密码
- 记住密码
- 登录使用cookies记住密码
- 记住密码相关问题!
- windoes 上Git bash 操作记住密码
- SVN记住密码
- (转)ASP.NET里面简单的记住用户名和密码
- 一个记住密码的功能实现
- 阻止浏览器记住密码功能干扰表单填充
- 防止浏览器记住用户名及密码的简单实用方法
- cookie实现记住密码功能
- demo6,记住密码提示框,总结