In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "Android uses SharedPreferences to achieve login registration and logout function", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Android uses SharedPreferences to achieve login and logout function" article.
I. the logic of this paper
The registration and login logic of this article is as follows:
1. Registration page: you can go directly to the login page with an account. If there is no account, fill in the account password, check that the account password is not empty, click to register now, save the account information, and jump to the login page.
2, login page: first read the cached account password and whether to remember the password, display the account, determine the sign to remember the password, empty or false, do not display the password, need to enter the password, for true will directly display the cached password, select whether to remember the password button, store this state in the cache, click login to jump to the home page.
3. Home page: first, take the cache to determine whether to log in or not. If there is no login, jump to the registration page. Click exit to log in, jump to the registration page; (2) Click to log out the account, clear all caches, and jump to the registration page.
II. Code
1. Layout interface
Registration page activity_register.xml
Login interface activity_login.xml
Main page
2.Activity logic
Logic of the registration page
Package com.example.yuan;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;/** * Created by Yuan on 17:08 on 2020-8-6. * package name: com.example.yuan * Class description: register (button click events directly use Activity as event listener) / public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {/ / get user name, nickname, password, confirm password private EditText reg_uname,reg_uname2,et_pwd,et_pwd2; / / get user name, nickname, password, confirm password value private String uname,pwd,pwd2; private SharedPreferences sp Private SharedPreferences.Editor editor;private Button btn_reg,btn_toLogin; @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_register); init (); / / get interface controls} / / UI component initialization and event binding private void init () {/ / UI component initialization reg_uname=findViewById (R.id.reg_uname) Et_pwd=findViewById (R.id.reg_pwd); et_pwd2=findViewById (R.id.reg_pwd2); btn_toLogin=findViewById (R.id.register_toLogin_btn); btn_reg=findViewById (R.id.register_btn); / / Click event binding btn_reg.setOnClickListener (this); btn_toLogin.setOnClickListener (this) } / / Click the event @ Override public void onClick (View v) {switch (v.getId ()) {case R.id.register_btn: / / get the parameter uname=reg_uname.getText (). ToString (). Trim (); pwd=et_pwd.getText (). ToString (). Trim () Pwd2=et_pwd2.getText () .toString () .trim (); / / judge if (uname.equals (") | | pwd.equals (")) {Toast.makeText (RegisterActivity.this, "username or password cannot be empty", Toast.LENGTH_SHORT) .show (); return } else if (! (pwd.equals (pwd2) {Toast.makeText (RegisterActivity.this, "two passwords are inconsistent", Toast.LENGTH_SHORT). Show ();} else {/ / loginInfo represents the file name sp=getSharedPreferences ("loginInfo", MODE_PRIVATE) / / get editor editor=sp.edit (); / / store registered user information editor.putString ("username", uname); editor.putString ("password", pwd); / / submit modification editor.commit () Toast.makeText (RegisterActivity.this, "registered successfully", Toast.LENGTH_SHORT). Show (); startActivity (new Intent (getApplicationContext (), LoginActivity.class));} break; case R.id.register_toLogin_btn:// jumps to the login page startActivity (new Intent (getApplicationContext (), LoginActivity.class)) Finish (); break;}
The logic of the login page
Package com.example.yuan;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity / * Created by Yuan on 16:12 on 2020-8-6. * package name: com.example.yuan * Class description: login * / public class LoginActivity extends AppCompatActivity {private EditText et_uname,et_upwd;// to get the user name, password private CheckBox isRemember; private Button login_btn; private String username,password,sp_name,sp_pwd; private Boolean sp_isRemember;// whether to remember the password flag private Context mContext; private SharedPreferences sp Private SharedPreferences.Editor editor; @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_login); init (); / / get interface controls / / read cache data sp=getSharedPreferences ("loginInfo", MODE_PRIVATE); sp_name=sp.getString ("username", "); sp_pwd=sp.getString (" password ",") Sp_isRemember=sp.getBoolean ("isRemember", false); / / if you have an account, directly display Log.d ("TAG", sp_name); et_uname.setText (sp_name); / / if you remember the password in the last login, this login is directly set to if (sp_isRemember.equals (true)) {et_upwd.setText (sp_pwd). IsRemember.setChecked (true);} mContext= LoginActivity.this / / Click event of login button (directly use anonymous inner class) login_btn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / start login, get the entered user name and password username = et_uname.getText () .toString () .trim () Password = et_upwd.getText (). ToString (). Trim (); / / TextUtils.isEmpty determines whether to enter if (TextUtils.isEmpty (username)) {Toast.makeText (mContext, "enter your mobile phone number", Toast.LENGTH_SHORT) .show () } else if (TextUtils.isEmpty (password)) {Toast.makeText (mContext, "Please enter password", Toast.LENGTH_SHORT) .show () / / determine whether the account password entered is consistent with that saved in SharedPreferences} else if (username.equals (sp_name) & & password.equals (sp_pwd)) {/ / consistent login success Toast.makeText (mContext, "login successful", Toast.LENGTH_SHORT) .show () / / determine whether the remember password button is selected, set isRemember to true, save the status sp=getSharedPreferences ("loginInfo", MODE_PRIVATE); / / get the editor editor=sp.edit () If (isRemember.isChecked ()) {/ / if the password is selected, save it to true, otherwise save it to false editor.putBoolean ("isRemember", true);} else {editor.putBoolean ("isRemember", false);} editor.putBoolean ("isLogin", true) / / Save login status editor.apply (); / / submit changes / / close this page after successful login to enter the home page / / jump to the main interface, and pass the login status to MainActivity Intent intent=new Intent (getApplicationContext (), MainActivity.class); intent.putExtra ("name", username) StartActivity (intent); finish (); / / destroy login interface} else if ((sp_pwd! = null & &! TextUtils.isEmpty (sp_pwd) & &! password.equals (sp_pwd)) {Toast.makeText (mContext, "entered username and password are inconsistent", Toast.LENGTH_SHORT). Show () } else {Toast.makeText (mContext, "this username does not exist", Toast.LENGTH_SHORT) .show ();} / / get the interface control private void init () {et_uname=findViewById (R.id.login_username); et_upwd=findViewById (R.id.login_password) IsRemember=findViewById (R.id.login_remember); login_btn=findViewById (R.id.login_btn);} / / Click event of the registration button (directly bound to the control) public void register (View view) {Intent intent=new Intent (getApplicationContext (), RegisterActivity.class); startActivity (intent);}}
The logic of the main page
Package com.example.yuan;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {private SharedPreferences sp; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) TextView textView=findViewById (R.id.text); Intent intent=getIntent (); String name=intent.getStringExtra ("name"); sp=getSharedPreferences ("loginInfo", MODE_PRIVATE); textView.setText ("Welcome," + name); Boolean isLogin=sp.getBoolean ("isLogin", false); if (! isLogin) {startActivity (new Intent (getApplicationContext (), RegisterActivity.class); finish () } else if (TextUtils.isEmpty (name)) {startActivity (new Intent (getApplicationContext (), LoginActivity.class)); finish ();}} public void Quit (View view) {sp=getSharedPreferences ("loginInfo", MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit (); editor.putBoolean ("isLogin", false); editor.commit () StartActivity (new Intent (getApplicationContext (), RegisterActivity.class)); finish ();} / * * @ param view * / public void zhuxiao (View view) {sp=getSharedPreferences ("loginInfo", MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit (); editor.clear (); editor.commit () Toast.makeText (MainActivity.this, logout successful, Toast.LENGTH_SHORT) .show (); startActivity (new Intent (getApplicationContext (), RegisterActivity.class)); finish () }} the above is about the content of this article on "how to log in and register with SharedPreferences in Android". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.