今天,我要分享给大家的是Android中常见的一个的登录注册的案例,我这里写的是简易版,如果大家有更精彩的拓展,可以自行发挥哦!

运行过程相信大家都已经心知肚明了,所以我在这里就直接发布代码了,其中有不理解的地方大家可以自行百度,也可以互相学习讨论。如有错误,麻烦大家在评论区留言,谢谢。

AndroidManifest.xml文件:

<applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.WeChat"><activity android:name=".LoginActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".RegisterActivity" /><activity android:name=".MainActivity"/></application>

布局代码:

styles.xml

<style name="hLine"><item name="android:layout_width">match_parent</item><item name="android:layout_height">1dp</item><item name="android:background">@color/black</item></style><style name="tvTwo"><item name="android:layout_width">wrap_content</item><item name="android:layout_height">wrap_content</item><item name="android:layout_marginLeft">20dp</item><item name="android:textColor">@color/black</item><item name="android:textSize">15sp</item></style><style name="etOne"><item name="android:layout_width">match_parent</item><item name="android:layout_height">wrap_content</item><item name="android:layout_marginLeft">30dp</item><item name="android:background">@null</item><item name="android:textColor">@color/black</item></style>

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="用户登录/LOGIN"android:textSize="30sp"android:textStyle="bold"android:textColor="@color/black"android:layout_marginTop="50dp"/><Viewstyle="@style/hLine"android:layout_marginTop="40dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="性   名:" /><EditTextandroid:id="@+id/lg_name"style="@style/etOne" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="密   码:" /><EditTextandroid:id="@+id/lg_psw"style="@style/etOne"android:inputType="textPassword"/></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="50dp"><Buttonandroid:id="@+id/btn_login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="25dp"android:layout_marginLeft="50dp"android:text="登 录"android:background="@color/gray"/><Buttonandroid:id="@+id/btn_register"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="25dp"android:layout_marginLeft="120dp"android:text="注 册"android:background="@color/gray"/></LinearLayout>
</LinearLayout>

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="用户注册"android:textColor="@color/black"android:textSize="40sp"/><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="姓名:" /><EditTextandroid:id="@+id/rg_name"style="@style/etOne" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="密码:" /><EditTextandroid:id="@+id/rg_psw"style="@style/etOne"android:inputType="textPassword" /></LinearLayout><View style="@style/hLine" /><Buttonandroid:id="@+id/btn_submit"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="120dp"android:gravity="center"android:text="提交"android:background="@color/gray"android:textSize="18sp" />
</LinearLayout>

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:text="祝你,登录成功,未来可期!"android:textColor="#E53935"android:textSize="30sp"/>
</LinearLayout>

Java代码:

LoginActivity.java

package com.example.wechat;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class LoginActivity extends AppCompatActivity {EditText lg_name,lg_psw;Button btn_login;Button btn_register;SQLiteDatabase db;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);lg_name=findViewById(R.id.lg_name);lg_psw=findViewById(R.id.lg_psw);btn_login=findViewById(R.id.btn_login);btn_register=findViewById(R.id.btn_register);//登录注册部分数据库db=SQLiteDatabase.openOrCreateDatabase(getCacheDir()+"/note",null);try {db.execSQL("create table user(username varchar(100),password varchar(100))");} catch (Exception e){e.printStackTrace();}SharedPreferences sharedPreferences=getSharedPreferences("user",0);lg_name.setText(sharedPreferences.getString("lg_name",""));lg_psw.setText(sharedPreferences.getString("lg_psw",""));btn_login.setOnClickListener(view -> {if (lg_name.getText().toString().equals("") || lg_psw.getText().toString().equals("")){Toast.makeText(LoginActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();return;}@SuppressLint("Recycle") Cursor cursor=db.rawQuery("select * from user where username='"+lg_name.getText().toString()+"'",null);if (cursor.moveToNext()){if (cursor.getString(1).equals(lg_psw.getText().toString())){SharedPreferences.Editor editor=getSharedPreferences("lg_name",0).edit();Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();startActivity(new Intent(LoginActivity.this,MainActivity.class));editor.apply();}else {Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();}}else {Toast.makeText(LoginActivity.this, "账号不存在", Toast.LENGTH_SHORT).show();}});btn_register.setOnClickListener(view -> startActivity(new Intent(LoginActivity.this,RegisterActivity.class)));}
}

RegisterActivity.java

package com.example.wechat;import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class RegisterActivity extends AppCompatActivity {EditText rg_name,rg_psw;Button btn_submit;SQLiteDatabase db;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);db=SQLiteDatabase.openOrCreateDatabase(getCacheDir()+"/note",null);rg_psw=findViewById(R.id.rg_psw);rg_name=findViewById(R.id.rg_name);btn_submit=findViewById(R.id.btn_submit);btn_submit.setOnClickListener(view -> {if (rg_name.getText().toString().equals("") || rg_psw.getText().toString().equals("")){Toast.makeText(RegisterActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();return;}@SuppressLint("Recycle") Cursor cursor=db.rawQuery("select * from user where username='"+rg_name.getText().toString()+"'",null);if (cursor.moveToNext()){Toast.makeText(RegisterActivity.this, "账号已存在", Toast.LENGTH_SHORT).show();}else {ContentValues contentValues = new ContentValues();contentValues.put("username", rg_name.getText().toString());contentValues.put("password", rg_psw.getText().toString());db.insert("user", null, contentValues);Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();startActivity(new Intent(RegisterActivity.this, LoginActivity.class));}});}
}

MainActivity.java

package com.example.wechat;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
}

像登录注册此类问题的案例,我们要特别注意逻辑性,涉及到数据量比较多、杂的话,我们可以用

笔将它们记录在本子上,世上无难事,只怕有心人,让我们一起加油吧!!!

Android之登录注册——简易版相关推荐

  1. Java基础-22总结登录注册IO版,数据操作流,内存操作流,打印流,标准输入输出流,转换流,随机访问流,合并流,序列化流,Properties...

    你需要的是什么,直接评论留言. 获取更多资源加微信公众号"Java帮帮" (是公众号,不是微信好友哦) 还有"Java帮帮"今日头条号,技术文章与新闻,每日更新 ...

  2. Android拦截黑名单(简易版)

    拦截黑名单的话,一般都是去系统数据库里面取值,判断来电手机号码或者短信号码是否在我黑名单数据中是否存在.如果存在就拦截.而我这里就投机取巧了,没有去数据库.只是简单的拦截,将数据写死了! 来上代码: ...

  3. 【Android笔记65】Android小案例之简易版的房贷计算器(附源代码)

    这篇文章,主要介绍如何使用Android实现一个简易版的房贷计算器小案例. 目录 一.房贷计算器 1.1.运行效果演示 1.2.前提准备 (1)等额本息和等额本金

  4. Android 实现 登录注册注销功能

    Android用SharedPreferences实现登录注册注销功能 前言 本文用SharedPreferences本地缓存账号信息来实现登录注册功能,以及退出注销功能. 一.本文逻辑 本文的注册登 ...

  5. Java基础-22总结登录注册IO版,数据操作流,内存操作流,打印流,标准输入输出流,转换流,随机访问流,合并流,序列化流,Properties

    你需要的是什么,直接评论留言. 获取更多资源加微信公众号"Java帮帮" (是公众号,不是微信好友哦) 还有"Java帮帮"今日头条号,技术文章与新闻,每日更新 ...

  6. Android用户登录注册界面

    用户登录注册界面开发及用户信息管理案例详解 刚开始接触Android编程,这算是我写的第一个简单工程,主要功能有:用户登录.注册.注销.修改密码.记住密码共5个基本操作,其内容涉及到以下几点: 1:B ...

  7. Android开发 登录注册设计

    用Android Studio 简单的实现登录注册 目录 一.登录界面 activity_login.xml 布局代码: login.java 代码: 二.注册界面 activity_register ...

  8. Android - 原生登录注册页面【仿】淘宝App

    bean文件夹:解析数据 LoginBean.java private String msg;private String code;private DataBean data;public Stri ...

  9. Node.js + Express 4.x + MongoDB 构建登录注册-简易用户管理(四)

    登录和注册的功能算实现了,下面封装DBHelp和增加一个简单的用户管理,这样增删查改就集齐了. 在routes文件夹下面新建DBHelp.js,代码如下: const MongoClient=requ ...

最新文章

  1. DeepChem | 基于图卷积预测分子的溶解度
  2. R将dataframe数据保存为csv文件
  3. 最近的生活[发点牢骚]
  4. WebService – 3.后台调用WebService,根级别上的数据无效
  5. Zabbix中文乱码问题
  6. VTK:几何对象之ShrinkCube
  7. 从边缘到云,万物互联时代Aruba的技术经
  8. oracle多列转换成树结构,如何将树“压缩”为Oracle中层次结构上的聚合数据?
  9. 机器学习算法的差异_我们的机器学习算法可放大偏差并永久保留社会差异
  10. deglitch 技术_fdc2214中文资料-技术参考.pdf
  11. python中rect用法_pyGame中rect对象的方法解释,pygame,Rect,详解
  12. 侏罗纪怪兽世界怎么登陆服务器未响应,全金属怪物一直登入不进去怎么办
  13. Randy Pausch_卡内基梅隆大学演讲--真正实现你的梦想
  14. Java 对象 、String 、JSON 互转
  15. matlab力学实验,Matlab在力学课程课堂教学和虚拟实验中的应用
  16. postgresql启动1053错误
  17. vim修改sessions存放目录_不拆机直接修改黑群晖的SN和MAC
  18. Java基础项目:小鲨鱼记账系统
  19. 爱招飞软件开发工具与 Arduino 与 ESP32 的关系
  20. 2021年度训练联盟热身训练赛第五场

热门文章

  1. 浅谈嵌入式MCU软件开发之S32K1xx系列MCU启动过程及重映射代码到RAM中运行方法详解
  2. JSP页面如何通过Form传递参数到另一个JSP页面
  3. Kaggle -Linear Regression with Time Series
  4. 云计算在未来一年的发展预测
  5. Python迷宫游戏
  6. 二分查找的左右逼近法
  7. 命令行导入mysql数据库
  8. iOS操作系统的层次架构和相关服务
  9. Unity3D射击类游戏制作第三节--游戏模型
  10. 守护安全|AIRIOT城市天然气综合管理解决方案