<?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"> <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>
package com.example.zuoye; 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(""); } } }