当前位置: 首页 > 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;避…...

大话软工笔记—需求分析概述

需求分析&#xff0c;就是要对需求调研收集到的资料信息逐个地进行拆分、研究&#xff0c;从大量的不确定“需求”中确定出哪些需求最终要转换为确定的“功能需求”。 需求分析的作用非常重要&#xff0c;后续设计的依据主要来自于需求分析的成果&#xff0c;包括: 项目的目的…...

IoT/HCIP实验-3/LiteOS操作系统内核实验(任务、内存、信号量、CMSIS..)

文章目录 概述HelloWorld 工程C/C配置编译器主配置Makefile脚本烧录器主配置运行结果程序调用栈 任务管理实验实验结果osal 系统适配层osal_task_create 其他实验实验源码内存管理实验互斥锁实验信号量实验 CMISIS接口实验还是得JlINKCMSIS 简介LiteOS->CMSIS任务间消息交互…...

IT供电系统绝缘监测及故障定位解决方案

随着新能源的快速发展&#xff0c;光伏电站、储能系统及充电设备已广泛应用于现代能源网络。在光伏领域&#xff0c;IT供电系统凭借其持续供电性好、安全性高等优势成为光伏首选&#xff0c;但在长期运行中&#xff0c;例如老化、潮湿、隐裂、机械损伤等问题会影响光伏板绝缘层…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

稳定币的深度剖析与展望

一、引言 在当今数字化浪潮席卷全球的时代&#xff0c;加密货币作为一种新兴的金融现象&#xff0c;正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而&#xff0c;加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下&#xff0c;稳定…...

鸿蒙DevEco Studio HarmonyOS 5跑酷小游戏实现指南

1. 项目概述 本跑酷小游戏基于鸿蒙HarmonyOS 5开发&#xff0c;使用DevEco Studio作为开发工具&#xff0c;采用Java语言实现&#xff0c;包含角色控制、障碍物生成和分数计算系统。 2. 项目结构 /src/main/java/com/example/runner/├── MainAbilitySlice.java // 主界…...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)

船舶制造装配管理现状&#xff1a;装配工作依赖人工经验&#xff0c;装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书&#xff0c;但在实际执行中&#xff0c;工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...

C/C++ 中附加包含目录、附加库目录与附加依赖项详解

在 C/C 编程的编译和链接过程中&#xff0c;附加包含目录、附加库目录和附加依赖项是三个至关重要的设置&#xff0c;它们相互配合&#xff0c;确保程序能够正确引用外部资源并顺利构建。虽然在学习过程中&#xff0c;这些概念容易让人混淆&#xff0c;但深入理解它们的作用和联…...

【从零学习JVM|第三篇】类的生命周期(高频面试题)

前言&#xff1a; 在Java编程中&#xff0c;类的生命周期是指类从被加载到内存中开始&#xff0c;到被卸载出内存为止的整个过程。了解类的生命周期对于理解Java程序的运行机制以及性能优化非常重要。本文会深入探寻类的生命周期&#xff0c;让读者对此有深刻印象。 目录 ​…...

纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join

纯 Java 项目&#xff08;非 SpringBoot&#xff09;集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...