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

告别SQL编写!用Dify打造你的专属数据库对话Agent(含提示词优化技巧)

从零构建智能数据库对话Agent&#xff1a;Dify实战与提示词深度优化指南 在数据驱动的决策时代&#xff0c;非技术用户与数据库之间的鸿沟一直是企业效率的隐形瓶颈。传统SQL查询需要专业知识门槛&#xff0c;而Dify平台的出现&#xff0c;让自然语言到SQL的转换变得触手可及。…...

多核编程避坑指南:为什么你的共享变量总是不听话?

多核编程避坑指南&#xff1a;为什么你的共享变量总是不听话&#xff1f; 想象一下这样的场景&#xff1a;你和同事同时编辑一份在线文档&#xff0c;两人都在某个单元格里输入数字并点击"保存"。理论上两次操作应该让数字增加两次&#xff0c;但最终结果可能只增加了…...

Anything V5服务优化指南:如何调整参数获得最佳生成效果

Anything V5服务优化指南&#xff1a;如何调整参数获得最佳生成效果 1. 理解Anything V5的核心参数 1.1 分辨率设置对生成效果的影响 Anything V5支持多种分辨率设置&#xff0c;但不同分辨率会直接影响生成速度和质量&#xff1a; 512x512&#xff1a;默认设置&#xff0c…...

实战指南:用快马平台生成团队统一的homebrew环境配置脚本,保障协作无忧

最近在团队协作中遇到了一个头疼的问题&#xff1a;新成员加入时&#xff0c;光是搭建开发环境就要折腾一整天。不同成员的电脑上软件版本参差不齐&#xff0c;导致"在我机器上能跑"的经典问题频繁出现。经过一番摸索&#xff0c;我发现用homebrew配合bash脚本可以完…...

百川2-13B-4bits量化模型精度实测:在OpenClaw复杂任务中的表现

百川2-13B-4bits量化模型精度实测&#xff1a;在OpenClaw复杂任务中的表现 1. 测试背景与实验设计 去年冬天第一次接触量化模型时&#xff0c;我曾天真地认为"4bits精度损失可以忽略不计"。直到用OpenClaw执行跨平台内容发布任务时&#xff0c;一个错误的文件路径让…...

UE5材质贴图避坑指南:为什么你的金属材质看起来不对劲?

UE5金属材质表现不佳的7个关键原因与解决方案 当你在UE5中精心制作的金属材质始终缺乏真实感时&#xff0c;问题往往隐藏在贴图交互与参数设置的细节中。本文将解剖金属材质表现不佳的典型症状&#xff0c;并提供可直接落地的调试方法。 1. 金属材质表现不佳的典型症状诊断 金属…...

OpenClaw多模型调度方案:GLM-4.7-Flash与本地小模型协同工作

OpenClaw多模型调度方案&#xff1a;GLM-4.7-Flash与本地小模型协同工作 1. 为什么需要多模型协同 去年冬天&#xff0c;当我第一次尝试用OpenClaw自动化处理周报时&#xff0c;发现一个尴尬的现象&#xff1a;用GLM-4.7-Flash这样的大模型处理简单表格整理&#xff0c;就像用…...

TSMaster实战:基于UDS BootLoader的ECU刷写上位机开发指南

1. TSMaster与UDS BootLoader刷写基础 第一次接触汽车电子刷写的朋友可能会被一堆术语搞晕&#xff0c;让我用最直白的方式解释&#xff1a;ECU就像汽车里的小电脑&#xff0c;BootLoader是它的"恢复模式"&#xff0c;而UDS协议就是和它对话的语言。TSMaster这个国产…...

颠覆式数据主权革命:WeChatMsg如何让你的聊天记录真正归属自己

颠覆式数据主权革命&#xff1a;WeChatMsg如何让你的聊天记录真正归属自己 【免费下载链接】WeChatMsg 提取微信聊天记录&#xff0c;将其导出成HTML、Word、CSV文档永久保存&#xff0c;对聊天记录进行分析生成年度聊天报告 项目地址: https://gitcode.com/GitHub_Trending/…...

HRN模型与PID控制结合:实时面部动画调节系统

HRN模型与PID控制结合&#xff1a;实时面部动画调节系统 1. 引言 想象一下&#xff0c;你正在制作一部动画电影&#xff0c;主角的面部表情需要精确到每一帧的微妙变化。传统的手工调整方式耗时耗力&#xff0c;而自动生成的表情又往往缺乏自然流畅的过渡。这就是为什么我们需…...