GitHub地址:https://github.com/1165863642/LoginDemo
直接贴代码<?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" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity">
1 package com.example.a11658.logindemo; 2 3 import android.content.SharedPreferences; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.text.Editable; 7 import android.text.TextUtils; 8 import android.text.TextWatcher; 9 import android.view.View;10 import android.widget.Button;11 import android.widget.CheckBox;12 import android.widget.EditText;13 import android.widget.Toast;14 15 public class MainActivity extends AppCompatActivity {16 17 Button btn_login, btn_cancel;18 EditText et_user, et_pass;19 CheckBox cb_remember;20 SharedPreferences spf;21 22 @Override23 protected void onCreate(Bundle savedInstanceState) {24 super.onCreate(savedInstanceState);25 setContentView(R.layout.activity_main);26 initView();27 }28 29 //初始化控件30 private void initView() {31 spf = getSharedPreferences("test", MODE_PRIVATE);32 //关联控件33 btn_cancel = findViewById(R.id.btn_cancel);34 btn_login = findViewById(R.id.btn_login);35 et_pass = findViewById(R.id.et_pass);36 et_user = findViewById(R.id.et_user);37 cb_remember = findViewById(R.id.cb_remember);38 et_user.setText(spf.getString("username", "111"));39 //点击事件40 btn_login.setOnClickListener(new View.OnClickListener() {41 @Override42 public void onClick(View v) {43 //登陆44 //1.获取用户名密码45 String username = et_user.getText().toString().trim();46 String password = et_pass.getText().toString().trim();47 //2.判断是否记住用户名48 if (cb_remember.isChecked()) { //判断CheckBox选中状态49 spf.edit().putString("username", username).commit();50 } else {51 spf.edit().clear().commit();52 }53 54 //3.判断用户名密码是否正确55 if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {56 if (username.equals("user") && password.equals("pass")) {57 Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();58 } else {59 Toast.makeText(MainActivity.this, "用户名密码不正确", Toast.LENGTH_SHORT).show();60 }61 } else {62 Toast.makeText(MainActivity.this, "输入框不能为空", Toast.LENGTH_SHORT).show();63 }64 }65 });66 67 btn_cancel.setOnClickListener(new View.OnClickListener() {68 @Override69 public void onClick(View v) {70 finish();71 }72 });73 74 et_pass.addTextChangedListener(new TextWatcher() {75 @Override76 public void beforeTextChanged(CharSequence s, int start, int count, int after) {77 //文字改变前78 Toast.makeText(MainActivity.this,"请输入", Toast.LENGTH_SHORT).show();79 }80 81 @Override82 public void onTextChanged(CharSequence s, int start, int before, int count) {83 //正在输入84 Toast.makeText(MainActivity.this,"正在输入", Toast.LENGTH_SHORT).show();85 }86 87 @Override88 public void afterTextChanged(Editable s) {89 //输入结束后90 Toast.makeText(MainActivity.this,"输入结束后", Toast.LENGTH_SHORT).show();91 }92 });93 }94 }
效果图::
涉及到的一些知识点 不懂的可以咨询我 扣:1165863642 共同学习