Android studio 编写一个登录页面,并且具有注册功能

这篇具有很好参考价值的文章主要介绍了Android studio 编写一个登录页面,并且具有注册功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

功能要求

1、创建登录界面,点击注册按钮,弹出注册窗口。
2、创建注册窗口,输入用户名和密码,在SQLite中存储用户名和密码。
3、注册成功,跳转到登录界面,进行登录。
4、注册成功,把用户名和密码保存到SharedPreferences中,登录时自动填充用户名和密码。

具体实现

初步思路

          登录页面具有两个输入框和两个按键,输入框分别用于获取用户名和密码,按键分为登录按键和注册按键,分别跳往不同的页面。登陆成功后,进入到欢迎界面。注册跳转到注册页面,注册成功后,将数据存储到SharedPreferences和数据中,返回登录页面将SharedPreferences中的数据填充到输入框中。

涉及的代码文件

  1. MainActivity.java :主界面
  2. Mysql.java:利用sqlite的SQLiteOpenHelper类创建数据库
  3. Register.java:注册页面
  4. Welcome.java:登陆成功后的欢迎界面:
  5. activity_main.xml:登录页面的布局文件
  6. activity_register.xml:注册页面的布局文件
  7. activity_welcome.xml:欢迎页面的布局文件
    一般在Android studio 中创建Activity都会自动在AndroidManifest.xml中好,本次代码中也不需要对AndroidManifest.xml做任何更改。

实现效果

1登录页面:
       这是我之前注册过后的账户,所以点击进去就自动填充好了信息。
Android studio 编写一个登录页面,并且具有注册功能
2.注册页面:
       注册页面写的非常简略,如果想实现更多的效果可以网上查查,也可以参照下我上一篇文章中的注册页面。
编写一个简单的andriod注册页面,并跳转后显示注册信息
Android studio 编写一个登录页面,并且具有注册功能
3.注册成功后:
       注册成功后会自动跳转到登录页面,并且填充信息。
Android studio 编写一个登录页面,并且具有注册功能
4.登录成功后:
       登陆成功后会将用户名取出来,并显示欢迎。
Android studio 编写一个登录页面,并且具有注册功能

代码文件

1.登录界面——MainActivity.java :

       这里登录比对数据的时候,我采用的方法是从数据库中查询与输入的用户名、密码相同的记录,若记录存在则登陆成功。不存在则提示用户用户名或密码输入错误。

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();
            }
        });
    }
}

这是对应的布局文件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>

2.注册界面——Register.java

       注册时,会先对用户名进行比对,若用户名存在则提醒用户名已存在。设置密码会比对两次输入的密码是否相同,不相同则发出提醒。

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();			//提示密码不一致
                    }

                }
            }


        });
    }
}

这是对应的布局文件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>

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",""));     //获取用户名
    }
}

这是对应的布局文件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>

4.数据库——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) {

    }
}

       代码可能有些简陋,考虑也可能没那么全面,但基本的功能还是可以的。暂时没发现什么大的问题。文章来源地址https://www.toymoban.com/news/detail-400578.html

到了这里,关于Android studio 编写一个登录页面,并且具有注册功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android Studio|使用SqLite实现一个简单的登录注册功能

    本学期学习了Android Studio这门课程,本次使用Android Studio自带的sqlite数据库实现一个简单的登录注册功能。 目录 一、了解什么是Android Studio? 二、了解什么是sqlite? 三、创建项目文件  四、创建活动文件和布局文件。 五、创建数据库,连接数据库  六、创建实体类,实现注

    2024年02月06日
    浏览(44)
  • 使用Android Studio创建第一个小项目(登录页面)

    仅供参考,学习使用 我这里了就直接创建一个模块了 然后选择Empty Activity 接下来直接点finish 我没有艺术细胞,所以画的比较差,大家不要介意 点击下面我图片的箭头处 然后点击split ####删掉我图片中的内容 然后点回design 点击旁边的TextView,拖动到方框中来 接着我们让这个

    2024年02月07日
    浏览(45)
  • uniapp-含有后端的登录注册页面编写

    数据库结构 表名:user 列名 数据类型 描述 id int 自增ID username varchar 用户名 password varchar 密码 nickname varchar 昵称 这个方案只保留了id、username、password和nickname四个字段,以最简单的方式存储用户基本信息。需要注意的是,密码应该进行安全处理(如加密),避免泄露敏感信息

    2024年02月06日
    浏览(29)
  • 使用javaweb实现登录注册页面,并且对功能和业务进行分层 用户登录成功跳转到主页并展示数据库的商品的信息

    一、Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据。 Servlet+JSP+JavaBean模式程序各个模块之间层次清晰,web开发推荐采用此种模式。 这里以一个最常用的用户登录

    2024年02月03日
    浏览(44)
  • Android Studio心得-创建登录注册项目

            首先先了解AndroidStudio是什么:Android Studio是一个由谷歌开发的Android应用程序开发环境,用于开发Android应用程序。它基于JetBrains IntelliJIDEA软件,并包含了许多定制化功能,包括易于使用的分析工具、内存分析工具和代码编辑器等,支持Java、Kotlin等多种编程语言。An

    2024年02月05日
    浏览(50)
  • Android开发实战——登录注册页面(附源码)

    效果图: 这段代码是一个简单的Android应用中的登录功能代码,下面逐行解释其功能和作用: 导入相关的类和包: 这些导入语句引入了用于构建Android应用界面、处理用户交互和数据存储的必要类和包。 定义一个名为 LoginActivity 的类,它继承自 AppCompatActivity ,表示这是一个用

    2024年02月11日
    浏览(37)
  • 使用原生js写一个简单的注册登录页面

    目录 1.注册页面 2.登录页面 1.首先是我们的注册页面 这是我们的html骨架  这是css样式 下面就是我们注册的js封装了         这里的函数调用直接写到了html里面的button事件上面了 这就是我的注册页面 ,当然各位要是觉得不好看也可以换成自己喜欢的图片; 2.登陆页面      

    2024年02月11日
    浏览(58)
  • 用JSP简单的写一个登录注册页面

    编写页面时先创建一个Dynamic web project,所有的jsp文件都放在WebContent文件夹下,java文件放在java Resource文件的src文件 因为主要用jsp因此这里用map数组暂替数据库对用户信息进行存储

    2023年04月17日
    浏览(43)
  • Android Studio —— Activity组件(课后作业:登录和注册App)

    运行效果图   主界面(初始),注册界面,登录界面,主界面(注册和登录之后) 实现步骤 1.设计主界面,编写activity_main.xml 注:(1) 按钮的格式是自己设计的,如下 注:(2)需编写strings.xml 2.创建两个activity(会自动创建对应的layout布局文件)   3.设计登录和注册界面,编写

    2024年02月05日
    浏览(37)
  • Android studio连接MySQL并完成简单的登录注册功能

    近期需要完成一个Android项目,那先从与数据库交互最简单的登陆注册开始吧,现记录过程如下: 此篇文章的小demo主要涉及数据库的连接,以及相应信息的查找与插入。 我已将源码上传至GitHub: https://github.com/changyan-maker/LoginApp 首先展示一下完成效果。 数据库设计: 数据库

    2024年01月17日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包