界面:
功能:
QQ登录界面,默认设置登录账号为admin,密码也为admin。
(1)、如果输入账号或密码其中任意一个错误直接显示密码错误请重试,此时会清空账号密码输入框中的内容,勾选与未勾选记住密码按钮,效果一样;
(2)、如果账号密码都输入正确没勾选记住密码按钮,则显示登陆成功,未保存;
(3)、如果账号密码都输入正确,且勾选了记住密码按钮,则显示登陆成功已保存,此时保存了密码,之后再重新进入app账号密码自动显示在屏幕的相应位置,保存数据均用SharedPreferences数据存储方式。
(4)、保存数据的文件及内容
Layout代码:
<?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" android:orientation="vertical" tools:context=".MainActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" android:src="@drawable/qq" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="30dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号:" android:textSize="30sp" /> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginRight="30dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" android:textSize="30sp" /> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="30dp"> <CheckBox android:id="@+id/cbRemember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="记住密码" android:textSize="20sp" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:onClick="btnLogin" android:text="登录" android:textSize="20sp" /> <Button android:id="@+id/btnExit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="取消" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
Activity代码:
package com.example.sp; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private EditText etName; private EditText etPassword; private CheckBox cbRemember; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etName = findViewById(R.id.etName); etPassword = findViewById(R.id.etPassword); cbRemember = findViewById(R.id.cbRemember); SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE); etName.setText(sp.getString("name", "")); etPassword.setText(sp.getString("password", "")); } public void btnLogin(View view) { if ("admin".equals(etName.getText().toString()) && "admin".equals(etPassword.getText().toString())) { if (cbRemember.isChecked()) { SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("name", etName.getText().toString()); editor.putString("password", etPassword.getText().toString()); editor.commit(); Toast.makeText(this, "登陆成功,已保存", Toast.LENGTH_SHORT).show(); } else { SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("name", ""); editor.putString("password", ""); editor.commit(); Toast.makeText(this, "登陆成功,未保存", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "密码错误,请重试", Toast.LENGTH_SHORT).show(); etName.setText(""); etPassword.setText(""); } } }