功能要求
1、創(chuàng)建登錄界面,點(diǎn)擊注冊(cè)按鈕,彈出注冊(cè)窗口。
2、創(chuàng)建注冊(cè)窗口,輸入用戶名和密碼,在SQLite中存儲(chǔ)用戶名和密碼。
3、注冊(cè)成功,跳轉(zhuǎn)到登錄界面,進(jìn)行登錄。
4、注冊(cè)成功,把用戶名和密碼保存到SharedPreferences中,登錄時(shí)自動(dòng)填充用戶名和密碼。
具體實(shí)現(xiàn)
初步思路
??????????登錄頁(yè)面具有兩個(gè)輸入框和兩個(gè)按鍵,輸入框分別用于獲取用戶名和密碼,按鍵分為登錄按鍵和注冊(cè)按鍵,分別跳往不同的頁(yè)面。登陸成功后,進(jìn)入到歡迎界面。注冊(cè)跳轉(zhuǎn)到注冊(cè)頁(yè)面,注冊(cè)成功后,將數(shù)據(jù)存儲(chǔ)到SharedPreferences和數(shù)據(jù)中,返回登錄頁(yè)面將SharedPreferences中的數(shù)據(jù)填充到輸入框中。
涉及的代碼文件
- MainActivity.java :主界面
- Mysql.java:利用sqlite的SQLiteOpenHelper類創(chuàng)建數(shù)據(jù)庫(kù)
- Register.java:注冊(cè)頁(yè)面
- Welcome.java:登陸成功后的歡迎界面:
- activity_main.xml:登錄頁(yè)面的布局文件
- activity_register.xml:注冊(cè)頁(yè)面的布局文件
- activity_welcome.xml:歡迎頁(yè)面的布局文件
一般在Android studio 中創(chuàng)建Activity都會(huì)自動(dòng)在AndroidManifest.xml中好,本次代碼中也不需要對(duì)AndroidManifest.xml做任何更改。
實(shí)現(xiàn)效果
1登錄頁(yè)面:
???????這是我之前注冊(cè)過(guò)后的賬戶,所以點(diǎn)擊進(jìn)去就自動(dòng)填充好了信息。
2.注冊(cè)頁(yè)面:
???????注冊(cè)頁(yè)面寫的非常簡(jiǎn)略,如果想實(shí)現(xiàn)更多的效果可以網(wǎng)上查查,也可以參照下我上一篇文章中的注冊(cè)頁(yè)面。
編寫一個(gè)簡(jiǎn)單的andriod注冊(cè)頁(yè)面,并跳轉(zhuǎn)后顯示注冊(cè)信息
3.注冊(cè)成功后:
???????注冊(cè)成功后會(huì)自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面,并且填充信息。
4.登錄成功后:
???????登陸成功后會(huì)將用戶名取出來(lái),并顯示歡迎。
代碼文件
1.登錄界面——MainActivity.java :
???????這里登錄比對(duì)數(shù)據(jù)的時(shí)候,我采用的方法是從數(shù)據(jù)庫(kù)中查詢與輸入的用戶名、密碼相同的記錄,若記錄存在則登陸成功。不存在則提示用戶用戶名或密碼輸入錯(cuò)誤。
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); //注冊(cè)按鈕
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); //建數(shù)據(jù)庫(kù)或者取數(shù)據(jù)庫(kù)
db = mysql.getReadableDatabase();
btnlogin.setOnClickListener(new View.OnClickListener() { //登錄事件
@Override
public void onClick(View v) {
String username = name.getText().toString();
String password = pwd.getText().toString(); //獲取用戶輸入的用戶名和密碼
//查詢用戶名和密碼相同的數(shù)據(jù)
Cursor cursor = db.query("logins",new String[]{"usname","uspwd"}," usname=? and uspwd=?",new String[]{username,password},null,null,null);
int flag = cursor.getCount(); //查詢出來(lái)的記錄項(xiàng)的條數(shù),若沒(méi)有該用戶則為0條
if(flag!=0){ //若查詢出的記錄不為0,則進(jìn)行跳轉(zhuǎn)操作
Intent intent = new Intent();
intent.setClass(MainActivity.this,Welcome.class); //設(shè)置頁(yè)面跳轉(zhuǎn)
SharedPreferences.Editor editor = sp2.edit();
cursor.moveToFirst(); //將光標(biāo)移動(dòng)到position為0的位置,默認(rèn)位置為-1
String loginname = cursor.getString(0);
editor.putString("Loginname",loginname);
editor.commit(); //將用戶名存到SharedPreferences中
startActivity(intent);
}
else{
Toast.makeText(MainActivity.this,"用戶名或密碼錯(cuò)誤!",Toast.LENGTH_LONG).show(); //提示用戶信息錯(cuò)誤或沒(méi)有賬號(hào)
}
}
});
btnreg.setOnClickListener(new View.OnClickListener() { //注冊(cè)事件
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,Register.class); //跳轉(zhuǎn)到注冊(cè)頁(yè)面
startActivity(intent);
Toast.makeText(MainActivity.this,"前往注冊(cè)!",Toast.LENGTH_SHORT).show();
}
});
}
}
這是對(duì)應(yīng)的布局文件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="注冊(cè)"
/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2.注冊(cè)界面——Register.java
???????注冊(cè)時(shí),會(huì)先對(duì)用戶名進(jìn)行比對(duì),若用戶名存在則提醒用戶名已存在。設(shè)置密碼會(huì)比對(duì)兩次輸入的密碼是否相同,不相同則發(fā)出提醒。
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); //設(shè)置初始密碼編輯框
usepwd2 = this.findViewById(R.id.usepwd2); //二次輸入密碼編輯框
submit = this.findViewById(R.id.submit); //注冊(cè)按鈕
mysql = new Mysql(this,"Userinfo",null,1); //建數(shù)據(jù)庫(kù)
db = mysql.getReadableDatabase();
sp = this.getSharedPreferences("useinfo",this.MODE_PRIVATE);
submit.setOnClickListener(new View.OnClickListener() {
boolean flag = true; //判斷用戶是否已存在的標(biāo)志位
@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)) { //判斷兩次輸入的密碼是否一致,若一致則繼續(xù),不一致則提醒密碼不一致
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); //跳轉(zhuǎn)到登錄頁(yè)面
startActivity(intent);
Toast.makeText(Register.this, "注冊(cè)成功!", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(Register.this, "密碼不一致!", Toast.LENGTH_LONG).show(); //提示密碼不一致
}
}
else{
Toast.makeText(Register.this, "用戶已存在!", Toast.LENGTH_LONG).show(); //提示密碼不一致
}
}
}
});
}
}
這是對(duì)應(yīng)的布局文件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"
/>
<!-- 確認(rèn)密碼部分 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="確認(rèn)密碼:"
/>
<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="注冊(cè)" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3.歡迎界面——Welcome.java
package 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","")); //獲取用戶名
}
}
這是對(duì)應(yīng)的布局文件activity_welcome.xml文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-400578.html
<?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>
4.數(shù)據(jù)庫(kù)——Mysql.java
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) {
}
}
???????代碼可能有些簡(jiǎn)陋,考慮也可能沒(méi)那么全面,但基本的功能還是可以的。暫時(shí)沒(méi)發(fā)現(xiàn)什么大的問(wèn)題。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-400578.html
到了這里,關(guān)于Android studio 編寫一個(gè)登錄頁(yè)面,并且具有注冊(cè)功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!