登陆界面
package com.example.admin.loginqq; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private EditText editText1,editText2; private CheckBox checkBox; private Button register,login; private SharedPreferences.Editor editor; private SharedPreferences pref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pref = PreferenceManager.getDefaultSharedPreferences(this); editText1 = findViewById(R.id.editText1); editText2 = findViewById(R.id.editText2); checkBox = findViewById(R.id.rememberPsd); register = findViewById(R.id.register); login = findViewById(R.id.login); //定义一个布尔型记住密码 boolean isRemember = pref.getBoolean("remember_password",false); 这里需要注意if()里面要判断之前储存的checkbox值,之前写的是if(isRemember),导致无法记住密码
if (pref.getBoolean("remember_password",true){
String username = pref.getString("username","");
String password = pref.getString("password","");
editText1.setText(username);
editText2.setText(password);
checkBox.setChecked(true);
}
//注册按钮
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,register.class);
startActivity(intent);
}
});
//登录按钮
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//读取输入的账号和密码 String inputText1 = editText1.getText().toString().trim(); String inputText2 = editText2.getText().toString().trim();
//读取注册的账号和密码 SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE); String username = pref.getString("username",""); String password = pref.getString("password","");
//判断输入的账号和密码是否与注册的账号密码一致
if (inputText1.equals(username) && inputText2.equals(password)){
editor = pref.edit();
if (checkBox.isChecked()){
editor.putBoolean("remember_password",true);
editor.putString("username",username);
editor.putString("password",password);
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}else{
editor.clear();
}
//勾选记住密码后,将账号密码以及记住密码的数据提交,以便进入程序后能够看到记住密码的功能
editor.apply();
//显示登录成功后退出程序,可以再次进入程序看是否显示密码
finish();
}else{
Toast.makeText(MainActivity.this, "账号或者密码输入错误", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#ffffff">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="QQ号/手机号/邮箱"
android:textColorHint="#949494"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="密码"
android:textColorHint="#949494"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:id="@+id/rememberPsd"
android:layout_marginLeft="20dp"
android:textColor="#25b6ed"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新用户注册"
android:id="@+id/register"
android:layout_marginLeft="165dp"
android:background="@android:color/transparent"
android:textColor="#25b6ed"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:background="#25b6ed"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textColor="#ffffff"/>
</LinearLayout>
注册界面
package com.example.admin.loginqq;
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.EditText;
import android.widget.Toast;
public class register extends AppCompatActivity {
private EditText editText1,editText2,editText3;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
editText3 = findViewById(R.id.editText3);
button = findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputText1 = editText1.getText().toString().trim();
String inputText2 = editText2.getText().toString().trim();
String inputText3 = editText3.getText().toString().trim();
if (inputText2.equals(inputText3)){
SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("username",inputText1);
editor.putString("password",inputText2);
editor.apply();
Toast.makeText(register.this, "恭喜您,注册成功,返回登录界面", Toast.LENGTH_LONG).show();
finish();
}else{
Toast.makeText(register.this, "两次输入的密码不同", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<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=".register"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="请输入您需要注册的账号"
android:textColorHint="#949494"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="请输入您需要注册的密码"
android:textColorHint="#949494"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="再次确认您输入的密码"
android:textColorHint="#949494"/>
<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:background="#25b6ed"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textColor="#ffffff"/>
</LinearLayout>