用户登录记住密码
来源:互联网 发布:手机贵金属软件 编辑:程序博客网 时间:2024/05/01 10:01
今天我们要做的任务就是我们平时登录时的界面,当点击记住密码的时候,下一次登录时就不必再输入账号和密码,点击登录直接进入。
首先将所用到的素材粘贴到drawable,将登录页面进行设计
<?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"
android:padding="@dimen/activity_horizontal_margin"
android:background="@drawable/logintop_roundbg">
<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/ctPass">
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etPassword">
<CheckBox
android:text="记住密码"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/cbIsRememberPass"
android:textSize="20sp"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:background="@drawable/btn_seclet"
android:onClick="login"
android:text="登录" />
</LinearLayout>
</RelativeLayout>
在Layout创建一个activity_main作为登录成功的欢迎界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.administrator.case_login.MainActivity">
<TextView
android:text="Welcome you"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:layout_marginRight="129dp"
android:layout_marginEnd="129dp"
android:layout_marginTop="215dp"
android:id="@+id/textView" />
</RelativeLayout>
在LoginActivity中配置
package com.example.administrator.case_login;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.SharedPreferencesCompat;
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 EditText etName;
private EditText etPassword;
private CheckBox cbIsRememberPass;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
sharedPreferences= getSharedPreferences("rememberpassword",Context.MODE_PRIVATE);
boolean isRemember=sharedPreferences.getBoolean("rememberpassword",false);
if(isRemember){
String name=sharedPreferences.getString("name","");
String password =sharedPreferences.getString("password","");
etName.setText(name);
etPassword.setText(password);
cbIsRememberPass.setChecked(true);
}
}
private void initViews(){
etName=(EditText)findViewById(R.id.etName);
etPassword=(EditText) findViewById(R.id.etPassword);
cbIsRememberPass=(CheckBox)findViewById(R.id.cbIsRememberPass);
}
public void login(View view){
String name=etName.getText().toString();
String password=etPassword.getText().toString();
if("admin".equals(name)&&"123456".equals(password)){
SharedPreferences.Editor editor=sharedPreferences.edit();
if(cbIsRememberPass.isChecked()){
editor.putBoolean("rememberpassword",true);
editor.putString("name",name);
editor.putString("password",password);
}else{
editor.clear();
}
editor.commit();
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(this,"账号或密码有误", Toast.LENGTH_LONG).show();
}
}
}
运行模拟器显示结果
点击记住密码后
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录(记住密码)
- 用户登录记住密码
- sonarqube版本升级注意事项
- JAVA设计模式之抽象工厂模式
- c++第四次实验-项目一
- 有时候错误也很美!
- 逐浪微站H5轻设计
- 用户登录记住密码
- PCIe INTx parse in ACPI
- HYSBZ 3944 Sum
- 例6.2-1&6.2-2
- Matlab 图像压缩编码—理论讲解
- maven3常用命令、java项目搭建、web项目搭建详细图解
- Java04-数组、进制转换与优化
- 2017蓝桥杯:等差素数列
- Linux下获得时间差,精确到微秒级别