In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "Android how to realize the login interface remember password function", in daily operation, I believe many people in Android how to realize the login interface remember password function there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation method, hope to answer "Android how to realize the login interface remember password function" doubts helpful! Next, please follow the small series to learn together!
tools needed
Check box control: CheckBox,
Second, SharedPreferences is used to store data. The reading and writing of this tool is relatively simple. It is explained in the comments in the code.
Implementation logic:
If you don't understand the logic, the code looks a little difficult.
1. Determine the Boolean information of CheckBox stored in SharedPreferences (if it is not read, the default condition is "No"). If the condition is "Yes"(both the information that can be read and the information that can be read are "Yes"), read the stored data (account and password) through SharedPreferences and write them into the corresponding text box.
2. When clicking the login button, judge whether CheckBox is checked. If the condition is "Yes", write the data in the accout and password boxes (String) and CheckBox data (Boolean) into SharedPreferences. If not checked, clear the data in SharedPreferences.
implementation code
I. UI interface
II. Fulfilment of functional parts
package com.example.broadcastbestpractice;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends BaseActivity { private EditText accountEdit; private EditText passwordEdit; private Button login; private SharedPreferences ref;//Read SharedPreferences data via ref private SharedPreferences.Editor editor;//editor Write data to SharedPreferences private CheckBox rememberPass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); pref= PreferenceManager.getDefaultSharedPreferences(this); accountEdit = (EditText) findViewById(R.id.account); passwordEdit = (EditText) findViewById(R.id.password); rememberPass=(CheckBox)findViewById(R.id.remember_pass); login = (Button) findViewById(R.id.login); boolean isRemember = ref.getBoolean("remember_password",false);//Read the information stored in "remember_password" when logging in last time, default to false if no read if(isRemember)//if read as true, write account and password, checkbox information into text box { String account=pref.getString("account",""); String password=pref.getString("password",""); accountEdit.setText(account); passwordEdit.setText(password); rememberPass.setChecked(true); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String accout = accountEdit.getText().toString(); String password = passwordEdit.getText().toString(); if (accout.equals("1") && password.equals("1")) { editor=pref.edit(); if(rememberPass.isChecked()){//If checkbox box is checked, write account, password, checkbox information editor.putBoolean("remember_password",true); editor.putString("account",accout); editor.putString("password",password); }else { editor.clear();//if not, clear the information stored in SharedPreferences } editor.apply(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else Toast.makeText(LoginActivity.this, "account or password is wrong", Toast.LENGTH_SHORT).show(); } }); }} At this point, the study on "Android how to realize the login interface remember password function" is over, I hope to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.