通过SharedPreferences实现存储用户的账号密码功能,下面是效果图
布局文件的login_top.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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:ems="10"
android:hint="@string/etName"
android:drawablePadding="10dp"
>
<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:ems="10"
android:hint="@string/etPassword"
android:inputType="textPassword"
android:drawablePadding="10dp"
>
</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/cblsRememberPass"
android:textSize="20sp"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:onClick="login"
android:text="@string/btnLogin" />
</LinearLayout>
</RelativeLayout>
activity_login.xml:
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 EditText etName,etPassword;
private SharedPreferences sharedPreference;
private CheckBox cblsRememberPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
sharedPreference=getSharedPreferences("rememberpassword",MODE_PRIVATE);
boolean isRemember=sharedPreference.getBoolean("rememberpassword",false);
if (isRemember){
String name=sharedPreference.getString("name","");
String password=sharedPreference.getString("password","");
etName.setText(name);
etPassword.setText(password);
cblsRememberPass.setChecked(true);
}
}
private void initViews() {
etName= (EditText) findViewById(R.id.etName);
etPassword= (EditText) findViewById(R.id.etPassword);
cblsRememberPass= (CheckBox) findViewById(R.id.cblsRememberPass);
}
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=sharedPreference.edit();
if (cblsRememberPass.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();
}
}
}
activity_main.xml
<?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="cn.edu.bzu.case_login.MainActivity">
<TextView
android:text="Welcome you"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="40sp"
android:layout_marginTop="263dp"
android:id="@+id/textView" />
</RelativeLayout>
LoginActivity部分的代码:
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 EditText etName,etPassword;
private SharedPreferences sharedPreference;
private CheckBox cblsRememberPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
sharedPreference=getSharedPreferences("rememberpassword",MODE_PRIVATE);
boolean isRemember=sharedPreference.getBoolean("rememberpassword",false);
if (isRemember){
String name=sharedPreference.getString("name","");
String password=sharedPreference.getString("password","");
etName.setText(name);
etPassword.setText(password);
cblsRememberPass.setChecked(true);
}
}
private void initViews() {
etName= (EditText) findViewById(R.id.etName);
etPassword= (EditText) findViewById(R.id.etPassword);
cblsRememberPass= (CheckBox) findViewById(R.id.cblsRememberPass);
}
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=sharedPreference.edit();
if (cblsRememberPass.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();
}
}
}
MainActivity部分的代码:
package cn.edu.bzu.case_login;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}