用户登录记住密码
来源:互联网 发布:杭州大树网络 编辑:程序博客网 时间:2024/05/01 13:18
当用户选中了记住密码复选框后,并且成功登录一次后,这个时候如果再重新启动登录界面,之前输入的用户名和密码就会显示在文本框中。
这里写代码片Login Activity:package cn.edu.bzu.case_login;import android.content.Intent;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity { private CheckBox CBPass; private EditText Name; private EditText Password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); this.read(); } public void save(View view) { Name = (EditText) findViewById(R.id.etName); Password = (EditText) findViewById(R.id.etPassword); CBPass = (CheckBox) findViewById(R.id.cbPass); String name = Name.getText().toString(); String password = Password.getText().toString(); Boolean cbpass = CBPass.isChecked(); if (name.equals("admin") && password.equals("123456")) { SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("etName", name); editor.putString("etPassword", password); editor.putBoolean("Click", cbpass); editor.commit(); Intent intent = new Intent(LoginActivity.this, WelActivity.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } public void read() { Name = (EditText) findViewById(R.id.etName); Password = (EditText) findViewById(R.id.etPassword); CBPass = (CheckBox) findViewById(R.id.cbPass); SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE); String name = sp.getString("etName", ""); String password = sp.getString("etPassword", ""); boolean cbpass = sp.getBoolean("Click", false); if (cbpass == true) { Password.setText(password); Name.setText(name); CBPass.setChecked(cbpass); } else { Password.setText(""); Name.setText(name); CBPass.setChecked(cbpass); } }}
这里写代码片activity_login.xml:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:background="@drawable/loginbg" android:id="@+id/activity_login" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="cn.edu.bzu.case_login.LoginActivity"> <include layout="@layout/login_top"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/deer" android:id="@+id/imageView" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /></RelativeLayout>
这里写代码片activity_wel.xml:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="Welcome You" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="208dp" android:textSize="25sp" android:id="@+id/welcome" /></RelativeLayout>
这里写代码片login_top.xml:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/logintop_roungbg" android:padding="@dimen/activity_horizontal_margin"> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_user" android:drawablePadding="10dp" android:ems="10" android:hint="@string/etName"> <requestFocus /> </EditText> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/etName" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_pass" android:drawablePadding="10dp" android:ems="10" android:hint="@string/etPass" android:inputType="textPassword"> <requestFocus /> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/etPassword"> <CheckBox android:id="@+id/cbPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="0.26" android:text="@string/btnSave" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@drawable/btn_select" android:onClick="save" android:text="@string/btnLogin" /> </LinearLayout></RelativeLayout>
0 0
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录(记住密码)
- 用户登录记住密码
- 第二章 C++概述
- [雅礼4-7]T3 最小割/乱搞
- Numpy.reshape函数解释
- CRC校验,以及使用实例
- 大白菜安装系统两种方式
- 用户登录记住密码
- 二十、Mysql 创建,删除,修改表
- [LeetCode OJ]Maximum Product Subarray
- phpstorm的操作
- 100. Same Tree
- 常见的图片格式及特点
- 【网络管理】链路层总结
- LeetCode-Easy部分中标签为 Dynamic Programming(DP) 53. Maximum Subarray
- Fragment与Activity交互方式使用Handler