当前位置: 首页 > news >正文

安卓简单登录

注意

有的朋友不知道登录咋写,这里我就简单给出相应代码,用的本地存储,没用网络请求,有需要可以替换成想要的,废话不多上代码

登录

import androidx.appcompat.app.AppCompatActivity;import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class LoginActivity extends AppCompatActivity {private EditText input_name;private EditText input_pwd;private TextView btn_login;private TextView btn_register;private SharedPreferences sharedPreferences;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_login = findViewById(R.id.btn_login);input_name = findViewById(R.id.input_name);input_pwd = findViewById(R.id.input_pwd);btn_register = findViewById(R.id.btn_register);// 初始化SharedPreferencessharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);btn_login.setOnClickListener(v -> {String username = input_name.getText().toString();String password = input_pwd.getText().toString();if (username.isEmpty() || password.isEmpty()) {Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();} else {// 从SharedPreferences中读取保存的用户名和密码String savedUsername = sharedPreferences.getString("username", "");String savedPassword = sharedPreferences.getString("password", "");if (savedUsername.isEmpty() || savedPassword.isEmpty()) {// 未注册,提示用户先进行注册Toast.makeText(LoginActivity.this, "用户未注册,请先注册", Toast.LENGTH_SHORT).show();} else if (username.equals(savedUsername) && password.equals(savedPassword)) {// 登录成功,跳转到下一个页面Intent intent = new Intent(LoginActivity.this, HomeActivity.class);startActivity(intent);} else {// 登录失败,显示错误信息Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();}}});btn_register.setOnClickListener(v -> {Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);startActivity(intent);});}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:background="@color/white"android:paddingLeft="12dp"android:paddingRight="12dp"android:orientation="vertical"tools:context=".LoginActivity"><ImageViewandroid:layout_width="80dp"android:layout_gravity="center"android:layout_marginTop="120dp"android:layout_height="80dp"android:src="@mipmap/ic_launcher"/><EditTextandroid:id="@+id/input_name"android:layout_width="match_parent"android:layout_height="60dp"android:hint="请输入用户名"android:textSize="16sp"android:layout_marginTop="30dp"android:maxLines="1"android:inputType="text"android:background="@drawable/rounded_border_shape"android:singleLine="true"android:paddingLeft="10dp"android:textColor="@color/black"/><EditTextandroid:id="@+id/input_pwd"android:layout_width="match_parent"android:layout_height="60dp"android:hint="请输入密码"android:textSize="16sp"android:layout_marginTop="20dp"android:maxLines="1"android:background="@drawable/rounded_border_shape"android:inputType="textPassword"android:paddingLeft="10dp"android:singleLine="true"android:textColor="@color/black"/><TextViewandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:paddingTop="10dp"android:paddingBottom="10dp"android:layout_marginTop="20dp"android:textColor="@color/white"android:textSize="18sp"android:background="@drawable/rounded_shape"android:text="登录"/><TextViewandroid:id="@+id/btn_register"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:paddingTop="10dp"android:paddingBottom="10dp"android:layout_marginTop="20dp"android:textColor="@color/white"android:textSize="18sp"android:background="@drawable/rounded_shape"android:text="立即注册"/>
</LinearLayout>

效果

下面是注册


import androidx.appcompat.app.AppCompatActivity;import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class RegisterActivity extends AppCompatActivity {private EditText input_name;private EditText input_pwd;private TextView btn_login;private TextView btn_register;private SharedPreferences sharedPreferences;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);btn_login = findViewById(R.id.btn_login);input_name = findViewById(R.id.input_name);input_pwd = findViewById(R.id.input_pwd);btn_register = findViewById(R.id.btn_register);sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);btn_register.setOnClickListener(v -> {String username = input_name.getText().toString();String password = input_pwd.getText().toString();if (username.isEmpty() || password.isEmpty()) {Toast.makeText(RegisterActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();} else {// 从SharedPreferences中读取保存的用户名String savedUsername = sharedPreferences.getString("username", "");if (savedUsername.equals(username)) {// 用户名已存在Toast.makeText(RegisterActivity.this, "用户名已存在,请直接登录", Toast.LENGTH_SHORT).show();} else {// 保存用户名和密码到SharedPreferencesSharedPreferences.Editor editor = sharedPreferences.edit();editor.putString("username", username);editor.putString("password", password);editor.apply();Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();// 跳转到登录页面Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);startActivity(loginIntent);}}});btn_login.setOnClickListener(v -> {Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);startActivity(loginIntent);});}
}

对应布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:background="@color/white"android:paddingLeft="12dp"android:paddingRight="12dp"android:orientation="vertical"tools:context=".LoginActivity"><ImageViewandroid:layout_width="80dp"android:layout_gravity="center"android:layout_marginTop="120dp"android:layout_height="80dp"android:src="@mipmap/ic_launcher"/><EditTextandroid:id="@+id/input_name"android:layout_width="match_parent"android:layout_height="60dp"android:hint="请输入用户名"android:textSize="16sp"android:layout_marginTop="30dp"android:maxLines="1"android:inputType="text"android:background="@drawable/rounded_border_shape"android:singleLine="true"android:paddingLeft="10dp"android:textColor="@color/black"/><EditTextandroid:id="@+id/input_pwd"android:layout_width="match_parent"android:layout_height="60dp"android:hint="请输入密码"android:textSize="16sp"android:layout_marginTop="20dp"android:maxLines="1"android:background="@drawable/rounded_border_shape"android:inputType="textPassword"android:paddingLeft="10dp"android:singleLine="true"android:textColor="@color/black"/><TextViewandroid:id="@+id/btn_register"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:paddingTop="10dp"android:paddingBottom="10dp"android:layout_marginTop="20dp"android:textColor="@color/white"android:textSize="18sp"android:background="@drawable/rounded_shape"android:text="立即注册"/><TextViewandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:paddingTop="10dp"android:paddingBottom="10dp"android:layout_marginTop="20dp"android:textColor="@color/white"android:textSize="18sp"android:background="@drawable/rounded_shape"android:text="去登录"/></LinearLayout>

效果图

用户登录成功获取所有用户信息

public class HomeActivity extends AppCompatActivity {private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_home);textView = findViewById(R.id.textView);getAllRegisteredUsers();}// 读取所有注册的用户信息private void getAllRegisteredUsers() {SharedPreferences sharedPrefs = getSharedPreferences("user_info", Context.MODE_PRIVATE);Map<String, ?> allEntries = sharedPrefs.getAll();JSONObject jsonObject = new JSONObject();for (Map.Entry<String, ?> entry : allEntries.entrySet()) {try {jsonObject.put(entry.getKey(), entry.getValue());} catch (JSONException e) {e.printStackTrace();}}textView.setText("当前注册的所有用户信息如下\n"+jsonObject.toString());}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context=".HomeActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_gravity="center"android:text="登录成功"android:layout_marginTop="60dp"android:textSize="30sp" /><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_gravity="center"android:text="登录成功"android:textSize="30sp" />
</LinearLayout>

最后加上一个rounded_border_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#FFFFFF" /> <!-- 填充颜色为白色,可以根据需要更改 --><strokeandroid:width="2dp"android:color="#787676" /><corners android:radius="10dp" />
</shape>

和 rounded_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#2196F3" /> <!-- 填充颜色为白色,可以根据需要更改 --><strokeandroid:width="2dp"android:color="#2196F3" /><corners android:radius="10dp" />
</shape>

以上就是整个登录注册代码,感激大家支持

相关文章:

安卓简单登录

注意 有的朋友不知道登录咋写&#xff0c;这里我就简单给出相应代码&#xff0c;用的本地存储&#xff0c;没用网络请求&#xff0c;有需要可以替换成想要的&#xff0c;废话不多上代码 登录 import androidx.appcompat.app.AppCompatActivity;import android.content.Context…...

【计算机网络】DNS/ICMP协议/NAT技术

文章目录 一、DNS(Domain Name System)1.DNS背景2.域名3.浏览器中输入url后,发生的事情 二、ICMP协议1.什么是ICMP协议2.ICM功能3.ICMP的报文格式4.ping命令5.traceroute命令 三、NAT技术1.NAT技术背景2.NAT IP转换过程3.NAPT4.NAT技术的缺陷5.NAT和代理服务器 四、TCP/IP五层模…...

2403C++,C++20协程通道

原文 通道是一个可用来连接协程,实现不同协程间通信的并发安全队列. Test fun test know channel() runBlocking<Unit> {val channel Channel<Int>()//生产者val producer GlobalScope.launch {var i 0while (true) {delay(1000)channel.send(i)println("…...

C语言从入门到实战——预处理详解

预处理详解 前言一、预定义符号1.1 __FILE__1.2__LINE__1.3 __DATE__1.4__TIME__1.5__STDC__ 二、 #define定义常量三、 #define定义宏四、 带有副作用的宏参数五、 宏替换的规则六、宏函数的对比七、 #和##7.1 #运算符7.2 ##运算符 八、 命名约定九、 #undef十、命令行定义十一…...

【LabVIEW FPGA】CIC滤波器

一、CIC滤波器应用概述 在通信数字信号上下变频时&#xff0c;经常会用到对数字信号的升采样和降采样&#xff0c;即通过CIC数字速率器实现变采样率。 二、滤波器IP 首先设置滤波器基本参数&#xff08;filter specification&#xff09; 滤波器类型&#xff08;Filter Type…...

砝码称重 蓝桥杯

在C中&#xff0c;fabs()和abs()都用于计算数字的绝对值&#xff0c;但它们之间有一些区别。 fabs(double x)&#xff1a;计算浮点数x的绝对值&#xff0c;返回一个double类型的结果。 abs(int x)&#xff1a;计算整数x的绝对值&#xff0c;返回一个int类型的结果。 数组的默…...

AmzTrends x TiDB Serverless:通过云原生改造实现全局成本降低 80%

本文介绍了厦门笛卡尔数据&#xff08;AmzTrends&#xff09;在面临数据存储挑战时&#xff0c;选择将其数据分析服务迁移到 TiDB Serverless 的思路和实践。通过全托管的数据库服务&#xff0c;AmzTrends 实现了全局成本降低 80% 的效果&#xff0c;同时也充分展示了 TiDB Ser…...

[最佳实践] Windows上构建一个和Linux类似的Terminal

感谢大佬批评指正&#xff0c;现已更新 preview Target&#xff1a;致力打造最赏心悦目Window下的终端&#xff0c;同时能够很接近Linux的使用习惯 key word&#xff1a;windows终端美化 windows terminal windows powershell 类似Linux下的Window终端 Window也能用ll windows…...

租赁系统|手机租赁软件|租赁系统功能开发

当如今的生活用品越来越多、交流更加便捷时&#xff0c;人们的消费需求也变得越来越丰富。不可避免地&#xff0c;我们会遇到这样一种情况&#xff1a;需要新的手机&#xff0c;但资金有限。此时&#xff0c;手机租赁小程序呼之欲出。这种创意不仅使我们能够充分利用各类闲置物…...

【设计模式 04】建造者模式

如果要构建的对象很复杂&#xff0c;那么可以将整个构建过程拆分成多个步骤&#xff0c;并为每一个步骤定义一个抽象的接口。并添加一个指导者用来控制构建产品的顺序和步骤。 Java实现&#xff1a; // 产品类 class Product {private String part1;private String part2;pub…...

Python使用错误总结

【1】cannot import name ‘ParameterSource’ from ‘click.core’ 其根本原因在于是black模块&#xff0c;其模块版本可能过时&#xff0c;升级black模块版本即可&#xff1a; pip install black --upgrade【2】partially initialized module ‘charset_normalizer’ has n…...

【Java EE初阶三十】JVM的简单学习

1. JVM 内存区域划分 一个运行起来的 Java 进程&#xff0c;就是一个 JVM 虚拟机&#xff0c;需要从操作系统申请一大块内存&#xff0c;就会把这个内存&#xff0c;划分成不同的区域&#xff0c;每个区域都有不同的作用. JVM 申请了一大块内存之后,也会划分成不同的内…...

thinkphp5水平分割表partition,以及查询操作

前言 先交代下背景,在一个项目中,有一个数据表有水平分表的需求。当时想找到一种方法&#xff0c;把对数据库的操作&#xff0c;写到一个模型里&#xff0c;通过去换模型属性中的table来达到代码不变操作的数据表变化的效果。 我们都知道&#xff0c;模型要想关联数据表的话&a…...

docker部署aria2-pro

前言 我平时有一些下载视频和一些资源文件的需求&#xff0c;有时候需要离线下载&#xff0c;也要速度比较快的方式 之前我是用家里的玩客云绝育之后不再写盘当下载机用的&#xff0c;但是限制很多 我发现了aria2 这个下载器非常适合我&#xff0c;而有个大佬又在原来的基础…...

vue中Mixins

使用 Mixins 的主要优点包括&#xff1a; 代码复用: 可以将常用的逻辑封装在 Mixin 中&#xff0c;然后在多个组件中重复使用。逻辑分离: 将不同功能的代码分开管理&#xff0c;使代码更加清晰和易于维护。灵活性: Mixins 允许你在组件中引入多个 Mixin&#xff0c;并且可以根…...

linux常用指令(定期更新)

linux常用指令 1.页相关页大小 2.系统参数3.启动参数4.网络参数查询网卡所属numa节点信息网络测速相关iperf测试sar监控网卡流量查看网卡txqueuelen和mtu抓包tcpdump 网络数据收发状态snmp协议栈netstat -i所有网口TX-OK、RX-OKnetstat-s查看各个协议的收发数据ethtool -S单个网…...

【项目】图书管理系统

目录 前言&#xff1a; 项目要求&#xff1a; 知识储备&#xff1a; 代码实现&#xff1a; Main&#xff1a; Books包&#xff1a; Book&#xff1a; BookList&#xff1a; Operate包&#xff1a; Operate: addOperate: deleteOperate: exitOperate: findOperate:…...

华为OD机试 - 疫情扩散时间计算 - 矩阵(Java 2024 C卷 200分)

目录 专栏导读一、题目描述二、输入描述三、输出描述四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明 华为OD机试 2024C卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷C卷&am…...

[数据集][图像分类]棉花叶子病害分类数据集2293张4类别

数据集类型&#xff1a;图像分类用&#xff0c;不可用于目标检测无标注文件 数据集格式&#xff1a;仅仅包含jpg图片&#xff0c;每个类别文件夹下面存放着对应图片 图片数量(jpg文件个数)&#xff1a;2293 分类类别数&#xff1a;4 类别名称:["diseased_cotton_leaf"…...

《辐射4》是一款什么样的游戏 怎样在mac电脑上玩到《辐射4》辐射4攻略 辐射4开局加点 怎么在Mac电脑玩Steam游戏

辐射4&#xff08;Fallout 4&#xff09;是由Bethesda开发的一款动作角色扮演类游戏&#xff0c;为《辐射》系列游戏作品的第四代&#xff0c;于2015年11月10日发行。游戏叙述了主角一家在核爆当天&#xff08;2077年10月23日&#xff09;&#xff0c;被Vault-Tec&#xff08;避…...

Spring Boot 实现流式响应(兼容 2.7.x)

在实际开发中&#xff0c;我们可能会遇到一些流式数据处理的场景&#xff0c;比如接收来自上游接口的 Server-Sent Events&#xff08;SSE&#xff09; 或 流式 JSON 内容&#xff0c;并将其原样中转给前端页面或客户端。这种情况下&#xff0c;传统的 RestTemplate 缓存机制会…...

visual studio 2022更改主题为深色

visual studio 2022更改主题为深色 点击visual studio 上方的 工具-> 选项 在选项窗口中&#xff0c;选择 环境 -> 常规 &#xff0c;将其中的颜色主题改成深色 点击确定&#xff0c;更改完成...

oracle与MySQL数据库之间数据同步的技术要点

Oracle与MySQL数据库之间的数据同步是一个涉及多个技术要点的复杂任务。由于Oracle和MySQL的架构差异&#xff0c;它们的数据同步要求既要保持数据的准确性和一致性&#xff0c;又要处理好性能问题。以下是一些主要的技术要点&#xff1a; 数据结构差异 数据类型差异&#xff…...

Nuxt.js 中的路由配置详解

Nuxt.js 通过其内置的路由系统简化了应用的路由配置&#xff0c;使得开发者可以轻松地管理页面导航和 URL 结构。路由配置主要涉及页面组件的组织、动态路由的设置以及路由元信息的配置。 自动路由生成 Nuxt.js 会根据 pages 目录下的文件结构自动生成路由配置。每个文件都会对…...

【单片机期末】单片机系统设计

主要内容&#xff1a;系统状态机&#xff0c;系统时基&#xff0c;系统需求分析&#xff0c;系统构建&#xff0c;系统状态流图 一、题目要求 二、绘制系统状态流图 题目&#xff1a;根据上述描述绘制系统状态流图&#xff0c;注明状态转移条件及方向。 三、利用定时器产生时…...

企业如何增强终端安全?

在数字化转型加速的今天&#xff0c;企业的业务运行越来越依赖于终端设备。从员工的笔记本电脑、智能手机&#xff0c;到工厂里的物联网设备、智能传感器&#xff0c;这些终端构成了企业与外部世界连接的 “神经末梢”。然而&#xff0c;随着远程办公的常态化和设备接入的爆炸式…...

蓝桥杯 冶炼金属

原题目链接 &#x1f527; 冶炼金属转换率推测题解 &#x1f4dc; 原题描述 小蓝有一个神奇的炉子用于将普通金属 O O O 冶炼成为一种特殊金属 X X X。这个炉子有一个属性叫转换率 V V V&#xff0c;是一个正整数&#xff0c;表示每 V V V 个普通金属 O O O 可以冶炼出 …...

C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)

名人说:路漫漫其修远兮,吾将上下而求索。—— 屈原《离骚》 创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊) 专栏介绍:《编程项目实战》 目录 一、为什么要开发一个日历程序?1. 深入理解时间算法2. 练习面向对象设计3. 学习数据结构应用二、核心算法深度解析…...

MySQL 主从同步异常处理

阅读原文&#xff1a;https://www.xiaozaoshu.top/articles/mysql-m-s-update-pk MySQL 做双主&#xff0c;遇到的这个错误&#xff1a; Could not execute Update_rows event on table ... Error_code: 1032是 MySQL 主从复制时的经典错误之一&#xff0c;通常表示&#xff…...

深入浅出Diffusion模型:从原理到实践的全方位教程

I. 引言&#xff1a;生成式AI的黎明 – Diffusion模型是什么&#xff1f; 近年来&#xff0c;生成式人工智能&#xff08;Generative AI&#xff09;领域取得了爆炸性的进展&#xff0c;模型能够根据简单的文本提示创作出逼真的图像、连贯的文本&#xff0c;乃至更多令人惊叹的…...