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--
This article mainly explains "how to achieve registration and login function based on Sqlite in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to register and log in based on Sqlite in Android.
Realization logic
The picture structure of the project is as follows
Code
User class
Public class User {private String name; / / username private String password; / / password public User (String name, String password) {this.name = name; this.password = password;} public String getName () {return name;} public void setName (String name) {this.name = name;} public String getPassword () {return password } public void setPassword (String password) {this.password = password;}}
DBOpenHelper class
Import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import java.util.ArrayList;public class DBOpenHelper extends SQLiteOpenHelper {private SQLiteDatabase db; public DBOpenHelper (Context context) {/ / Open database super (context, "db_test", null,1) / / 1: context, 2: database name, 3: allows us to return a Cursor,4 when querying data: version number of the current database db = getReadableDatabase () } @ Override public void onCreate (SQLiteDatabase db) {/ / create a table (user) statement db.execSQL ("CREATE TABLE IF NOT EXISTS user (" + / / PRIMARY key sets id as the primary key, AUTOINCREMENT sets the id column to grow "_ id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT," + / / text text type "password TEXT)") } @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {/ / rewrite upgrade db.execSQL ("DROP TABLE IF EXISTS user"); onCreate (db);} public void add (String name,String password) {/ / rewrite add db.execSQL ("INSERT INTO user (name,password) VALUES (?)", new Object [] {name,password}) } public void delete (String name,String password) {/ / rewrite delete db.execSQL ("DELETE FROM user WHERE name = AND password =" + name+password);} public void updata (String password) {/ / rewrite update db.execSQL ("UPDATE user SET password =?", new Object [] {password});} public ArrayList getAllData () {/ / return the table information as a list ArrayList list = new ArrayList () Cursor cursor = db.query ("user", null,null,null,null,null, "name DESC"); / / 1 table name, 2 columns, 3 rows, 4 rows, 5 specified columns for filtering, 6 further filtering. 7 sort the information (desc reverse order) while (cursor.moveToNext ()) {/ / traverse String name = cursor.getString (cursor.getColumnIndex ("name")); / / move to the name column and read String password = cursor.getString ("password"); list.add (new User (name,password)) / / add to the list of user} return list;// return list}}
Activity_login.xml
LoginActivity
Import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.content.Intent;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;public class LoginActivity extends AppCompatActivity implements View.OnClickListener {private DBOpenHelper mDBOpenHelper; private EditText mEtLoginactivityUsername; private EditText mEtLoginactivityPassword; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_login); initView (); mDBOpenHelper = new DBOpenHelper (this);} private void initView () {/ / initialize control mEtLoginactivityUsername = findViewById (R.id.et_loginactivity_username); mEtLoginactivityPassword = findViewById (R.id.et_loginactivity_password) / / set click event listener} public void onClick (View view) {switch (view.getId ()) {/ / Jump to the registration interface case R.id.tv_loginactivity_register: startActivity (new Intent (this, RegisterActivity.class)); finish (); break Case R.id.bt_loginactivity_login: String name = mEtLoginactivityUsername.getText () .toString () .trim (); / / .trim () delete the spaces on both sides String password = mEtLoginactivityPassword.getText () .toString () .trim () If (! TextUtils.isEmpty (name) & &! TextUtils.isEmpty (password)) {/ / TextUtils.isEmpty () the input box is empty or you hit the space bar a few times and the method will return true ArrayList data = mDBOpenHelper.getAllData (); / / data is the user information boolean match = false; for (int I = 0) in the obtained user table I < data.size (); iTunes +) {/ / compare User user = data.get (I); / / get the first user information in data if (name.equals (user.getName ()) & & password.equals (user.getPassword () {/ / compare the information with the input information match = true Break;} else {match = false;}} if (match) {Toast.makeText (this, "login successful", Toast.LENGTH_SHORT) .show () Intent intent = new Intent (this, MainActivity.class); startActivity (intent); finish (); / destroy this Activity} else {Toast.makeText (this, "user name or password is incorrect, please re-enter", Toast.LENGTH_SHORT) .show () }} else {Toast.makeText (this, "Please enter your username or password", Toast.LENGTH_SHORT). Show ();} break;}
Activity_register.xml
RegisterActivity
Import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {private DBOpenHelper mDBOpenHelper; private Button mBtRegisteractivityRegister; private ImageView mIvRegisteractivityBack; private EditText mEtRegisteractivityUsername; private EditText mEtRegisteractivityPassword @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_register); initView (); mDBOpenHelper = new DBOpenHelper (this);} private void initView () {mEtRegisteractivityUsername = findViewById (R.id.et_registeractivity_username); mEtRegisteractivityPassword = findViewById (R.id.et_registeractivity_password) } public void onClick (View view) {switch (view.getId ()) {case R.id.iv_registeractivity_back: / / return the login page Intent intent1 = new Intent (this, LoginActivity.class); startActivity (intent1); finish (); break Case R.id.bt_registeractivity_register: / / Registration button / / get the user name, password and CAPTCHA entered by the user String username = mEtRegisteractivityUsername.getText (). ToString () .trim (); String password = mEtRegisteractivityPassword.getText () .toString () .trim () / / Registration verification if (! TextUtils.isEmpty (username) & &! TextUtils.isEmpty (password)) {mDBOpenHelper.add (username, password); / / add user name and password to the table in the database Intent intent2 = new Intent (this, LoginActivity.class); startActivity (intent2) Finish (); Toast.makeText (this, "Verification passed, Registration successful, Toast.LENGTH_SHORT). Show ();} else {Toast.makeText (this," incomplete Information, Registration failed ", Toast.LENGTH_SHORT). Show ();} break }}}
Realize the effect
At this point, I believe you have a deeper understanding of "Android how to achieve registration and login functions based on Sqlite". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.