Android 实现记住账号密码
首先创建一个界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity"
android:orientation="vertical">
<EditText
android:id="@+id/loginEdt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"/>
<EditText
android:id="@+id/passwordEdt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"/>
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
<CheckBox
android:id="@+id/saveChb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"/>
</LinearLayout>
先附上源代码
package com.example.administrator.ch05_2;
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.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private Button loginBtn;
private EditText loginEdt;
private EditText passwordEdt;
private CheckBox saveChb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginEdt = findViewById(R.id.loginEdt);
passwordEdt = findViewById(R.id.passwordEdt);
loginBtn = findViewById(R.id.loginBtn);
saveChb = findViewById(R.id.saveChb);
loginBtn.setOnClickListener(this);
SharedPreferences sp2=getSharedPreferences("Logindb",MODE_PRIVATE);
if(sp2.getBoolean("save",false)==true ){ //判断是否写入了数值save==true
getDB();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.loginBtn:
if(saveChb.isChecked()){ //当多选按钮按下时执行报损数据
saveDB();
}
else {
clearDB();
}
if(loginEdt.getText().toString().trim().length()==0){
Toast.makeText(LoginActivity.this,"用户名密码不能为空",Toast.LENGTH_LONG).show();
}
else {
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
}
finish();
break;
}
}
//清除
private void clearDB(){
SharedPreferences sp=getSharedPreferences("Logindb",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.clear();
editor.commit();
}
//保存数据
private void saveDB(){
SharedPreferences sp=getSharedPreferences("Logindb",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("loginEdt",loginEdt.getText().toString());
editor.putString("passwordEdt",passwordEdt.getText().toString());
editor.putBoolean("save",true);
editor.commit(); //写入数据
Toast.makeText(LoginActivity.this,"sd",Toast.LENGTH_LONG).show();
}
//读取数据
private void getDB(){
SharedPreferences sp=getSharedPreferences("Logindb",MODE_PRIVATE);
String name= sp.getString("loginEdt","");
String password=sp.getString("passwordEdt","");
loginEdt.setText(name);
passwordEdt.setText(password);
}
}
还没有评论,来说两句吧...