AndroidStudio-实现登录界面(数据存储在SQLite)

 

要求:每种错误信息采用Toast进行提示

(1)未注册的用户不能进行登录;

(2)用户名和密码不能为空;

(3)用户名不能重复;

一、创建新工程

 点击next

修改名字 ,language看自己情况修改,sdk最好选21,这样21之后的都可以用,location自己改,点击finish

点击左上角的Android,切换成project

创建新的activity 

 修改名字,点击finish,同样的,再创建一个注册Activity

在AndroidMainfest.xml文件中将登录界面设为主界面

<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界面设计

插入图片,名字只能是小写

Login:ps:复制的时候得把注释去掉

<?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"//界面背景图
    android:orientation="vertical">//布局方式,vertical为垂直布局,horizontal为水平布局
    <LinearLayout
        android:layout_marginLeft="30dp"//容器中的左边距
        android:layout_marginRight="30dp"//容器中的右边距
        android:layout_width="match_parent"//和父容器一样大小
        android:layout_height="0dp"//按比例分配大小
        android:layout_weight="1"
        android:orientation="vertical">
        <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:layout_marginTop="30dp"//容器中的上边距
                android:textSize="25sp"//sp主要是字体大小
                android:textColor="#000000"
                android:layout_weight="1"/>
            <EditText
                android:id="@+id/userName"//如果要调用,就得加id
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="请输入用户名"
                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="请输入密码"
                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"//按钮上的英文可以展现大小写,默认为true,即显示的都是大写
            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="注册"
            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="用户名:"
            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="请输入用户名"
            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="请输入密码"
            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="确认"
            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>

三、逻辑设计

先建立数据类,建立一个只包含用户名和密码的user类。

同样,创建一个DatabaseHelper类,包含对数据库的一些基本操作(仅有创建数据库、更新数据库和数据的的插入和查询) 

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 {
    //创建一个数据库
    private SQLiteDatabase db;

    public DatabaseHelper(@Nullable Context context) {
        super(context, "db_test", null, 1);
        db = getReadableDatabase();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        //在第一次创建数据库的时候,创建一些字段
        String sql = "create table user(_id integer, name varchar(50), password varchar(40))";
        db.execSQL(sql);//sql语句的执行函数
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //如果这个表中存在user,我们可以先把他去掉,然后重新创建
        String sql = "DROP TABLE IF EXISTS user";
        db.execSQL(sql);
        onCreate(db);
    }
    //为使项目结构更加紧凑,我们在此类中编写增删改查的函数,因为只有登录和注册界面,因此只涉及到写入数据库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(){//查询数据库
        ArrayList<User> list = new ArrayList<User>();
        //查询数据库中的数据,并将这些数据按照降序的情况排列
        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);

        //找到各个控件
        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);

        //注册监听事件
        btn_ready.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())){
                        flag = true;
                        break;
                    }else{
                        flag = false;
                    }
                }
                //判断用户名和密码是否为空
                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, "注册成功", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(Register.this, "用户名已被注册", Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    Toast.makeText(Register.this, "用户名与密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //监听返回按钮
        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部分逻辑代码:

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, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    Toast.makeText(Login.this, "用户名与密码不能为空", 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);
    }
}

到这里就基本实现了一个登陆界面

  • 21
    点赞
  • 223
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
《嘟嘟音乐》是我自己写的Android,若有不足之处请大家谅解 1.首页实现读取本地Music本地文件夹中的MP3文件然后放进listview里面 2.实现用户用户的登录注册页面,登录过后用户可以进行自己的信息修改,实现了服务器的数据交互问题 3.实现了管理员登录,使用的是与Tomcat服务器进行数据交互验证信息的正确性,我使用的是myeclipse进行布置的服务器信息,此时使用的是SQLserver 2008 数据存储的管理员的信息,读取完以后然后返回到管理界面 4.在嘟嘟音乐的管理界面,我实现了策划菜单以及卡片式布局来管理普通用户信息,以及用户信息的增删改查。查询使用的是以UserId或者昵称迷糊查询的方式进行查询的。 5.我使用的SQL server2008 的数据库文件我已经全部导出了,大家可以自行进行导入 6.我使用的myeclipse的项目是Servlet进行的验证app管理员信息的邓丽,项目我已经全部导出。放在压缩包里面 7.我使用的是Android Studio,我把文件布局截图放在压缩包里面,还有几个需要注意的地方,特别的坑,尤其是大家需要注意build.gridle(app)这里面大家需要注册导包,你并且配置好自己的SDK。大家还需要注意就是Android的注册文件里面也需要注意,关于一些权限的问题 8.哈哈,大概的的就写到这里了,写的不好的地方大家多见谅,我也是在学习阶段,把自己的写的东西给大家贡献出来以供大家参考学习使用。我还在压缩包里面放置了视频演示的链接信息,大家可以看看。如果感觉写的不错的话,请好评哦。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值