今天对Android记住密码以及自动登录做出了处理
首先进行了对与login页面的优化:
package com.example.math; /* * 登录界面 * */ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.preference.PreferenceManager; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.example.math.repositiory.user_res; public class LoginActivity extends AppCompatActivity implements View.OnClickListener { private SharedPreferences pref; private SharedPreferences.Editor editor; private CheckBox rememberCb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); initData(); savePassword(); } private void savePassword() { pref = PreferenceManager.getDefaultSharedPreferences(this); EditText user_name = findViewById(R.id.userName); EditText user_password = findViewById(R.id.userPassword); rememberCb = findViewById(R.id.login_cb_ji); boolean isRemember = pref.getBoolean("remember",true); if(isRemember){ String account = pref.getString("account",""); String password = pref.getString("password",""); user_name.setText(account); user_password.setText(password); rememberCb.setChecked(true); } } private void initData() { Button login_l = findViewById(R.id.login); TextView bt_zhu = findViewById(R.id.login_zhu); TextView bt_xiu = findViewById(R.id.login_xiu); login_l.setOnClickListener(this); bt_zhu.setOnClickListener(this); bt_xiu.setOnClickListener(this); } @SuppressLint("HandlerLeak") public Handler myHandler = new Handler(Looper.myLooper()) { @Override public void handleMessage(@NonNull Message msg) { if (msg.what == 1) { Intent intent1 = new Intent(getApplicationContext(), UserMainActivity.class); startActivity(intent1); finish(); Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show(); } } }; @SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { switch (v.getId()) { case R.id.login: EditText user_name = findViewById(R.id.userName); EditText user_password = findViewById(R.id.userPassword); rememberCb = findViewById(R.id.login_cb_ji); String userName = user_name.getText().toString(); CustomApplication app = (CustomApplication) getApplication(); app.setValue(userName); login(user_name.getText().toString(), user_password.getText().toString()); editor = pref.edit(); if (rememberCb.isChecked()) { editor.putBoolean("remember", true); editor.putString("account", userName); editor.putString("password", user_password.getText().toString()); } else { editor.apply(); } break; case R.id.login_zhu: startActivity(new Intent(getApplicationContext(), register_Activity.class)); break; case R.id.login_xiu: startActivity(new Intent(getApplicationContext(), retrieve_Activity.class)); break; } } /** * 登录功能 */ public void login(String name, String password) { new Thread() { @Override public void run() { boolean aa; try { aa = user_res.user_login(name, password); } catch (Exception e) { throw new RuntimeException(e); } int msg = 0; if (aa) { msg = 1; } myHandler.sendEmptyMessage(msg); } }.start(); } }
对于记住密码以及通过手机唯一标识自动登录还在研究中。
标签:05,16,id,user,2023,import,login,password,android From: https://www.cnblogs.com/JJTyyds/p/17406562.html