?
要求:每種錯(cuò)誤信息采用Toast進(jìn)行提示
(1)未注冊(cè)的用戶(hù)不能進(jìn)行登錄;
(2)用戶(hù)名和密碼不能為空;
(3)用戶(hù)名不能重復(fù);
一、創(chuàng)建新工程
?點(diǎn)擊next
修改名字 ,language看自己情況修改,sdk最好選21,這樣21之后的都可以用,location自己改,點(diǎn)擊finish
點(diǎn)擊左上角的Android,切換成project
創(chuàng)建新的activity?
?修改名字,點(diǎn)擊finish,同樣的,再創(chuàng)建一個(gè)注冊(cè)Activity
在AndroidMainfest.xml文件中將登錄界面設(shè)為主界面
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyLogin"
tools:targetApi="31">
<activity
android:name=".Register"
android:exported="false" />
<activity
android:name=".Login"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="false"/>
</application>
二、UI界面設(shè)計(jì)
插入圖片,名字只能是小寫(xiě)
Login:ps:復(fù)制的時(shí)候得把注釋去掉
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout//線性布局
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img"http://界面背景圖
android:orientation="vertical">//布局方式,vertical為垂直布局,horizontal為水平布局
<LinearLayout
android:layout_marginLeft="30dp"http://容器中的左邊距
android:layout_marginRight="30dp"http://容器中的右邊距
android:layout_width="match_parent"http://和父容器一樣大小
android:layout_height="0dp"http://按比例分配大小
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"http://大小包裹內(nèi)容
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="用戶(hù)名:"
android:layout_marginTop="30dp"http://容器中的上邊距
android:textSize="25sp"http://sp主要是字體大小
android:textColor="#000000"
android:layout_weight="1"/>
<EditText
android:id="@+id/userName"http://如果要調(diào)用,就得加id
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入用戶(hù)名"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="密碼:"
android:textSize="25sp"
android:layout_marginTop="30dp"
android:textColor="#000000"
android:layout_weight="1"/>
<EditText
android:id="@+id/userpassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:hint="請(qǐng)輸入密碼"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_weight="2"
android:inputType="textWebPassword"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"
android:textAllCaps="false"http://按鈕上的英文可以展現(xiàn)大小寫(xiě),默認(rèn)為true,即顯示的都是大寫(xiě)
android:textColor="#FFFFFF"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:textSize="25sp" />
<Button
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:textSize="25sp"
android:textColor="#FFFFFF"
android:textAllCaps="false"
android:text="注冊(cè)"
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Register:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/img">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="用戶(hù)名:"
android:layout_marginTop="30dp"
android:textSize="25sp"
android:textColor="#000000"
android:layout_weight="1"/>
<EditText
android:id="@+id/userName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入用戶(hù)名"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="密碼:"
android:textSize="25sp"
android:layout_marginTop="30dp"
android:textColor="#000000"
android:layout_weight="1"/>
<EditText
android:id="@+id/userpassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:hint="請(qǐng)輸入密碼"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_weight="2"
android:inputType="textWebPassword"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:textColor="#000000"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textSize="25sp"
android:textAllCaps="false"
android:text="確認(rèn)"
android:id="@+id/reday"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:textColor="#000000"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textSize="25sp"
android:textAllCaps="false"
android:text="取消"
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
三、邏輯設(shè)計(jì)
先建立數(shù)據(jù)類(lèi),建立一個(gè)只包含用戶(hù)名和密碼的user類(lèi)。
同樣,創(chuàng)建一個(gè)DatabaseHelper類(lèi),包含對(duì)數(shù)據(jù)庫(kù)的一些基本操作(僅有創(chuàng)建數(shù)據(jù)庫(kù)、更新數(shù)據(jù)庫(kù)和數(shù)據(jù)的的插入和查詢(xún))?
User:
package com.example.mylogin;
public class User {
private int id;
private String name;
private String password;
public User(String name,String password){
super();
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User{id ="+ id + ", name = "+ name +",password ="+password +"}";
}
}
DatabaseHelper:
package com.example.mylogin;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class DatabaseHelper extends SQLiteOpenHelper {
//創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)
private SQLiteDatabase db;
public DatabaseHelper(@Nullable Context context) {
super(context, "db_test", null, 1);
db = getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
//在第一次創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候,創(chuàng)建一些字段
String sql = "create table user(_id integer, name varchar(50), password varchar(40))";
db.execSQL(sql);//sql語(yǔ)句的執(zhí)行函數(shù)
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//如果這個(gè)表中存在user,我們可以先把他去掉,然后重新創(chuàng)建
String sql = "DROP TABLE IF EXISTS user";
db.execSQL(sql);
onCreate(db);
}
//為使項(xiàng)目結(jié)構(gòu)更加緊湊,我們?cè)诖祟?lèi)中編寫(xiě)增刪改查的函數(shù),因?yàn)橹挥械卿浐妥?cè)界面,因此只涉及到寫(xiě)入數(shù)據(jù)庫(kù)insert和query的操作
public void insert(String name,String password ){
db.execSQL("insert into user(name,password)VALUES(?,?)",new Object[]{name,password});
}
public ArrayList<User> getAllDATA(){//查詢(xún)數(shù)據(jù)庫(kù)
ArrayList<User> list = new ArrayList<User>();
//查詢(xún)數(shù)據(jù)庫(kù)中的數(shù)據(jù),并將這些數(shù)據(jù)按照降序的情況排列
Cursor cursor = db.query("user",null,null,null,null,null,"name DESC");
while(cursor.moveToNext()){
int index_name = cursor.getColumnIndex("name");
int index_password = cursor.getColumnIndex("password");
String name = cursor.getString(index_name);
String password = cursor.getString(index_password);
list.add(new User(name,password));
}
return list;
}
}
Register部分的邏輯代碼:
package com.example.mylogin;
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.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class Register extends AppCompatActivity {
private DatabaseHelper mSQLite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//找到各個(gè)控件
Button btn_ready = findViewById(R.id.reday);
Button btn_back = findViewById(R.id.back);
EditText ed_name = findViewById(R.id.userName);
EditText ed_password = findViewById(R.id.userpassword);
//注冊(cè)監(jiān)聽(tīng)事件
btn_ready.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//獲取輸入的用戶(hù)名和密碼
String name = ed_name.getText().toString().trim();
String password = ed_password.getText().toString().trim();
//獲取數(shù)據(jù)庫(kù)數(shù)據(jù),判斷用戶(hù)名是否已存在
ArrayList<User> data = mSQLite.getAllDATA();
boolean flag = false;
for(int i = 0; i < data.size(); i++){
User userdata = data.get(i);
if(name.equals(userdata.getName())){
flag = true;
break;
}else{
flag = false;
}
}
//判斷用戶(hù)名和密碼是否為空
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){
if(!flag){
mSQLite.insert(name, password);
Intent intent1 = new Intent(Register.this, login.class);
startActivity(intent1);
finish();
Toast.makeText(Register.this, "注冊(cè)成功", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Register.this, "用戶(hù)名已被注冊(cè)", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(Register.this, "用戶(hù)名與密碼不能為空", Toast.LENGTH_SHORT).show();
}
}
});
//監(jiān)聽(tīng)返回按鈕
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent2 = new Intent(Register.this, login.class);
startActivity(intent2);
finish();
}
});
mSQLite = new DatabaseHelper(Register.this);
}
}
Login部分邏輯代碼:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-779992.html
package com.example.mylogin;
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.Toast;
import java.util.ArrayList;
public class Login extends AppCompatActivity {
private DatabaseHelper mSQLite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btn_login = findViewById(R.id.login);
Button btn_register = findViewById(R.id.register);
EditText ed_name = findViewById(R.id.userName);
EditText ed_password = findViewById(R.id.userpassword);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = ed_name.getText().toString().trim();
String password = ed_password.getText().toString().trim();
ArrayList<User> data = mSQLite.getAllDATA();
boolean flag = false;
for(int i = 0; i < data.size(); i++){
User userdata = data.get(i);
if(name.equals(userdata.getName())&&password.equals(userdata.getPassword())){
flag = true;
break;
}else{
flag = false;
}
}
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){
if(flag){
Intent intent1 = new Intent(Login.this, MainActivity.class);
startActivity(intent1);
finish();
Toast.makeText(Login.this, "登錄成功", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Login.this, "用戶(hù)名或密碼不正確", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(Login.this, "用戶(hù)名與密碼不能為空", Toast.LENGTH_SHORT).show();
}
}
});
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent2 = new Intent(Login.this, Register.class);
startActivity(intent2);
finish();
}
});
mSQLite = new DatabaseHelper(Login.this);
}
}
到這里就基本實(shí)現(xiàn)了一個(gè)登陸界面文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-779992.html
到了這里,關(guān)于AndroidStudio-實(shí)現(xiàn)登錄界面(數(shù)據(jù)存儲(chǔ)在SQLite)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!