1、创建登录界面,点击注册按钮,弹出注册窗口。
2、创建注册窗口,输入用户名和密码,在SQLite中存储用户名和密码。
3、注册成功,跳转到登录界面,进行登录。
4、注册成功,把用户名和密码保存到SharedPreferences中,登录时自动填充用户名和密码。
登录页面具有两个输入框和两个按键,输入框分别用于获取用户名和密码,按键分为登录按键和注册按键,分别跳往不同的页面。登陆成功后,进入到欢迎界面。注册跳转到注册页面,注册成功后,将数据存储到SharedPreferences和数据中,返回登录页面将SharedPreferences中的数据填充到输入框中。
1登录页面:
这是我之前注册过后的账户,所以点击进去就自动填充好了信息。
2.注册页面:
注册页面写的非常简略,如果想实现更多的效果可以网上查查,也可以参照下我上一篇文章中的注册页面。
编写一个简单的andriod注册页面,并跳转后显示注册信息
3.注册成功后:
注册成功后会自动跳转到登录页面,并且填充信息。
4.登录成功后:
登陆成功后会将用户名取出来,并显示欢迎。
这里登录比对数据的时候,我采用的方法是从数据库中查询与输入的用户名、密码相同的记录,若记录存在则登陆成功。不存在则提示用户用户名或密码输入错误。
package com.example.test06; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText name,pwd; Button btnlogin,btnreg; Mysql mysql; SQLiteDatabase db; SharedPreferences sp1,sp2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = this.findViewById(R.id.name); //用户名输入框 pwd = this.findViewById(R.id.pwd); //密码输入框 btnlogin = this.findViewById(R.id.login); //登录按钮 btnreg = this.findViewById(R.id.reg); //注册按钮 sp1 = this.getSharedPreferences("useinfo",this.MODE_PRIVATE); sp2 = this.getSharedPreferences("username",this.MODE_PRIVATE); name.setText(sp1.getString("usname",null)); pwd.setText(sp1.getString("uspwd",null)); mysql = new Mysql(this,"Userinfo",null,1); //建数据库或者取数据库 db = mysql.getReadableDatabase(); btnlogin.setOnClickListener(new View.OnClickListener() { //登录事件 @Override public void onClick(View v) { String username = name.getText().toString(); String password = pwd.getText().toString(); //获取用户输入的用户名和密码 //查询用户名和密码相同的数据 Cursor cursor = db.query("logins",new String[]{"usname","uspwd"}," usname=? and uspwd=?",new String[]{username,password},null,null,null); int flag = cursor.getCount(); //查询出来的记录项的条数,若没有该用户则为0条 if(flag!=0){ //若查询出的记录不为0,则进行跳转操作 Intent intent = new Intent(); intent.setClass(MainActivity.this,Welcome.class); //设置页面跳转 SharedPreferences.Editor editor = sp2.edit(); cursor.moveToFirst(); //将光标移动到position为0的位置,默认位置为-1 String loginname = cursor.getString(0); editor.putString("Loginname",loginname); editor.commit(); //将用户名存到SharedPreferences中 startActivity(intent); } else{ Toast.makeText(MainActivity.this,"用户名或密码错误!",Toast.LENGTH_LONG).show(); //提示用户信息错误或没有账号 } } }); btnreg.setOnClickListener(new View.OnClickListener() { //注册事件 @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this,Register.class); //跳转到注册页面 startActivity(intent); Toast.makeText(MainActivity.this,"前往注册!",Toast.LENGTH_SHORT).show(); } }); } }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273这是对应的布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.3" android:gravity="center" android:textSize="18dp" android:text="用户名:"/> <EditText android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="20dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:textSize="18dp" android:layout_weight="0.3" android:text="密 码:"/> <EditText android:id="@+id/pwd" android:layout_width="0dp" android:layout_height="wrap_content" android:inputType="textPassword" android:layout_weight="1" android:layout_marginRight="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="5dp" android:text="登录" /> <Button android:id="@+id/reg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="5dp" android:text="注册" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778注册时,会先对用户名进行比对,若用户名存在则提醒用户名已存在。设置密码会比对两次输入的密码是否相同,不相同则发出提醒。
package com.example.test06; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Register extends AppCompatActivity { EditText usename,usepwd,usepwd2; Button submit; Mysql mysql; SQLiteDatabase db; SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); usename = this.findViewById(R.id.usename); //用户名编辑框 usepwd = this.findViewById(R.id.usepwd);//设置初始密码编辑框 usepwd2 = this.findViewById(R.id.usepwd2); //二次输入密码编辑框 submit = this.findViewById(R.id.submit);//注册按钮 mysql = new Mysql(this,"Userinfo",null,1); //建数据库 db = mysql.getReadableDatabase(); sp = this.getSharedPreferences("useinfo",this.MODE_PRIVATE); submit.setOnClickListener(new View.OnClickListener() { boolean flag = true; //判断用户是否已存在的标志位 @Override public void onClick(View v) { String name = usename.getText().toString();//用户名 String pwd01 = usepwd.getText().toString();//密码 String pwd02 = usepwd2.getText().toString();//二次输入的密码 String sex = "";//性别 if(name.equals("")||pwd01 .equals("")||pwd02.equals("")){ Toast.makeText(Register.this, "用户名或密码不能为空!!", Toast.LENGTH_LONG).show(); } else{ Cursor cursor = db.query("logins",new String[]{"usname"},null,null,null,null,null); while (cursor.moveToNext()){ if(cursor.getString(0).equals(name)){ flag = false; break; } } if(flag==true){ //判断用户是否已存在 if (pwd01.equals(pwd02)) {//判断两次输入的密码是否一致,若一致则继续,不一致则提醒密码不一致 ContentValues cv = new ContentValues(); cv.put("usname",name); cv.put("uspwd",pwd01); db.insert("logins",null,cv); SharedPreferences.Editor editor = sp.edit(); editor.putString("usname",name); editor.putString("uspwd",pwd01); editor.commit(); Intent intent = new Intent(); intent.setClass(Register.this,MainActivity.class); //跳转到登录页面 startActivity(intent); Toast.makeText(Register.this, "注册成功!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(Register.this, "密码不一致!", Toast.LENGTH_LONG).show();//提示密码不一致 } } else{ Toast.makeText(Register.this, "用户已存在!", Toast.LENGTH_LONG).show();//提示密码不一致 } } } }); } }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384这是对应的布局文件activity_register.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".Register"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 用户名部分 --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/usename" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <!-- 密码部分 --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="密 码:" /> <EditText android:id="@+id/usepwd" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> <!-- 确认密码部分 --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="确认密码:" /> <EditText android:id="@+id/usepwd2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> <Button android:id="@+id/submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="注册" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657package com.example.test06; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.TextView; public class Welcome extends AppCompatActivity { SharedPreferences sp; TextView showhello; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); sp = this.getSharedPreferences("username", this.MODE_PRIVATE); //获取sharepreferences showhello = this.findViewById(R.id.mainword); //显示欢迎 showhello.setText("欢迎你!"+sp.getString("Loginname","")); //获取用户名 } }
123456789101112131415161718192021这是对应的布局文件activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".Welcome"> <TextView android:id="@+id/mainword" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:textSize="22dp" tools:ignore="MissingConstraints" /> </androidx.constraintlayout.widget.ConstraintLayout> 123456789101112131415
package com.example.test06; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class Mysql extends SQLiteOpenHelper { public Mysql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { String sql = "create table logins(id integer primary key autoincrement,usname text,uspwd text)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
123456789101112131415161718192021222324代码可能有些简陋,考虑也可能没那么全面,但基本的功能还是可以的。暂时没发现什么大的问题。
相关知识
在 Visual Studio 中生成跨平台移动应用
移动应用开发做登录界面(app手机登录界面)
基于Android Studio如何实现 购物商城 案例(简单易上手)
花店登录页面设计
基于Android studio的花店app设计与开发案例
Android Studio实现花店App
Android 移动应用基础教程(Android Studio)(第2版)学习笔记及源码
Android移动应用开发教程①
请介绍如何在Android平台上使用Java语言开发一个集成了用户登录、图书查询以及预约功能的移动图书馆客户端,并详细阐述在开发过程中如何确保应用的安全性。
移动应用开发
网址: Android studio 编写一个登录页面,并且具有注册功能 https://m.huajiangbk.com/newsview948773.html
上一篇: UML设计系列(3):时序图 |
下一篇: JavaWeb实现简单的用户注册 |