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

安卓如何书写注册和登录界面

一、如何跳转一个活动

左边的是本活动名称, 右边的是跳转界面活动名称

Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();

 

二、如果在不同的界面传递参数

//发送消息
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.apply(); //接收消息
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String savedUsername = sharedPreferences.getString("username", "");
String savedPassword = sharedPreferences.getString("password", "");

三、如何让图片是按钮

<ImageButtonandroid:id="@+id/imageButton"android:layout_width="50dp"android:layout_height="50dp"android:src="@drawable/s7" />

四、登录界面

1、创建一个LoginActivity类,然后创建对应的界面文件.xml

2、注册登录界面,并让登录界面其成为主界面

        <activityandroid:name=".LoginActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".MainActivity"/><activity android:name=".RegisterActivity"/>

3、书写登录界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="20dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="账号:"android:textSize="20dp"/><EditTextandroid:id="@+id/usernameEditText"android:layout_width="180dp"android:layout_height="wrap_content"android:textSize="20dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码:"android:textSize="20dp"/><EditTextandroid:id="@+id/passwordEditText"android:layout_width="180dp"android:layout_height="wrap_content"android:textSize="20dp"/></LinearLayout><TextViewandroid:layout_width="wrap_content"android:layout_height="20dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><CheckBoxandroid:id="@+id/rememberPasswordCheckbox"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="记住密码"android:layout_gravity="start"android:layout_marginStart="16dp"android:layout_marginTop="8dp"/><TextViewandroid:layout_width="80dp"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/zcButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="点击注册" /></LinearLayout><TextViewandroid:layout_width="wrap_content"android:layout_height="20dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><Buttonandroid:id="@+id/loginButton"android:layout_width="200dp"android:layout_height="wrap_content"android:text="确认登录" /></LinearLayout></LinearLayout>

4、书写登录活动代码

package com.example.yaodian;import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class LoginActivity extends AppCompatActivity {EditText usernameEditText;EditText passwordEditText;Button loginButton;Button zcButton;CheckBox rememberPasswordCheckbox ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login);rememberPasswordCheckbox = findViewById(R.id.rememberPasswordCheckbox);usernameEditText = findViewById(R.id.usernameEditText);passwordEditText = findViewById(R.id.passwordEditText);loginButton = findViewById(R.id.loginButton);zcButton = findViewById(R.id.zcButton);loginButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String inputUsername = usernameEditText.getText().toString();String inputPassword = passwordEditText.getText().toString();SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);String savedUsername = sharedPreferences.getString("username", "");String savedPassword = sharedPreferences.getString("password", "");// 在这里添加登录验证逻辑if (inputUsername.equals(savedUsername) && inputPassword.equals(savedPassword)) {Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();Intent intent = new Intent(LoginActivity.this, MainActivity.class);startActivity(intent);finish();// 跳转到 MainActivity 或其他操作} else {new AlertDialog.Builder(LoginActivity.this).setTitle("提示").setMessage("登录失败,用户名或密码错误").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).show();}}});zcButton.setOnClickListener((l) -> {Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);startActivity(intent);finish();});//        // 监听复选框状态变化
//        rememberPasswordCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                // 根据复选框状态来处理记住密码逻辑
//                if (isChecked) {
//
//                } else {
//
//                }
//            }
//        });}
}

五、注册界面

同登录界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="20dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="       账号:"android:textSize="20dp"/><EditTextandroid:id="@+id/usernameEditText"android:layout_width="180dp"android:layout_height="wrap_content"android:textSize="20dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="       密码:"android:textSize="20dp"/><EditTextandroid:id="@+id/passwordEditText"android:layout_width="180dp"android:layout_height="wrap_content"android:textSize="20dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="确认密码:"android:textSize="20dp"/><EditTextandroid:id="@+id/password2EditText"android:layout_width="180dp"android:layout_height="wrap_content"android:textSize="20dp"/></LinearLayout><TextViewandroid:layout_width="wrap_content"android:layout_height="50dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><Buttonandroid:id="@+id/registerButton"android:layout_width="200dp"android:layout_height="wrap_content"android:text="确认注册" /></LinearLayout></LinearLayout>
package com.example.yaodian;import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class RegisterActivity extends AppCompatActivity {private EditText usernameEditText;private EditText passwordEditText;private EditText password2EditText;private Button registerButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.register_activity);usernameEditText = findViewById(R.id.usernameEditText);passwordEditText = findViewById(R.id.passwordEditText);password2EditText = findViewById(R.id.password2EditText);registerButton = findViewById(R.id.registerButton);registerButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String username = usernameEditText.getText().toString().trim();String password = passwordEditText.getText().toString().trim();String password2 = password2EditText.getText().toString().trim();if (username.isEmpty() || password.isEmpty()) {// 弹出“请输入账号和密码”的对话框new AlertDialog.Builder(RegisterActivity.this).setTitle("提示").setMessage("请输入账号和密码").setPositiveButton("确定", null).show();} else if (!password.equals(password2)) {// 弹出“两次密码不一样,请重新输入”的对话框new AlertDialog.Builder(RegisterActivity.this).setTitle("提示").setMessage("两次密码不一样,请重新输入").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 清空密码和确认密码输入框passwordEditText.setText("");password2EditText.setText("");}}).show();} else {// 保存注册信息到 SharedPreferencesSharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);SharedPreferences.Editor editor = sharedPreferences.edit();editor.putString("username", username);editor.putString("password", password);editor.apply();Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);startActivity(intent);finish();}}});}
}

六、完整工程代码

链接:https://pan.baidu.com/s/13tm-AsAsJEmfJjbwyX0a3Q
提取码:生日

需要提取码的 加QQ:1482728006,说明来意,付费即可获取工程

相关文章:

安卓如何书写注册和登录界面

一、如何跳转一个活动 左边的是本活动名称&#xff0c; 右边的是跳转界面活动名称 Intent intent new Intent(LoginActivity.this, RegisterActivity.class); startActivity(intent); finish(); 二、如果在不同的界面传递参数 //发送消息 SharedPreferences sharedPreferen…...

黄仁勋的AI时代:英伟达GPU革命的狂欢与挑战

在最近的COMPUTEX 2024大会上&#xff0c;英伟达创始人黄仁勋发布了最新的Blackwell GPU。这次发布不仅标志着英伟达在AI领域的又一次飞跃&#xff0c;也展示了其对未来技术发展的战略规划。本文将详细解析英伟达最新技术的亮点&#xff0c;探讨其在AI时代的市场地位和未来挑战…...

Linux云计算架构师涨薪班课程内容包含哪些?

第一阶段&#xff1a;Linux云计算运维初级工程师 目标 云计算工程师&#xff0c;Linux运维工程师都必须掌握Linux的基本功&#xff0c;这是一切的根本&#xff0c;必须全部掌握&#xff0c;非常重要&#xff0c;有了这些基础&#xff0c;学习上层业务和云计算等都非常快&#x…...

c语言:自定义类型(枚举、联合体)

前言&#xff1a; c语言中中自定义类型不仅有结构体&#xff0c;还有枚举、联合体等类型&#xff0c;上一期我们详细讲解了结构体的初始化&#xff0c;使用&#xff0c;传参和内存对齐等知识&#xff0c;这一期我们来介绍c语言中的其他自定义类型枚举和联合体的知识。 1.位段 …...

2024年适合GISer参加的全国性比赛

作为一名GISer&#xff0c;在校期间参加GIS比赛&#xff0c;不仅能够锻炼和提升自己的GIS专业水平&#xff0c;例如软件操作、开发能力等&#xff1b;还能加强自己团队协作能力、组织能力和沟通能力&#xff0c;此外&#xff0c;还可以给简历加分&#xff0c;增强职场竞争力。 …...

番外篇-用户购物偏好标签BP-推荐算法ALS

引言 推荐系统式信息过载所采用的措施&#xff0c;面对海量的数据信息&#xff0c;从中快速推荐出符合用户特点的物品。 推荐系统是自动化的通过分析用户对历史行为数据&#xff0c;完成用户的个性化建模&#xff0c;从而主动给用户推荐能够满足他们兴趣和需求的软件系统。 数…...

气膜体育馆的防火性能分析—轻空间

随着现代体育事业的蓬勃发展&#xff0c;气膜体育馆因其建设快捷、成本低廉、使用灵活等优势&#xff0c;逐渐在全球范围内受到广泛关注。然而&#xff0c;对于这种新型建筑形式&#xff0c;防火性能一直是人们关注的焦点之一。轻空间将详细探讨气膜体育馆的防火性能&#xff0…...

什么台灯对眼睛好?一文给你分享具体什么台灯对眼睛好!

什么台灯对眼睛好&#xff1f;随着学生们最近陆续返校&#xff0c;家长们和孩子们都忙于开学初的准备工作&#xff0c;而眼睛的健康自然也是他们考虑的一部分。这也是护眼台灯在近年来变得非常普及的原因之一。我自己一直是一个近视的人&#xff0c;而且日常用眼时间也相当长。…...

python-bert模型基础笔记0.1.00

python-bert模型基础笔记0.1.00 bert的适合的场景bert多语言和中文模型bert模型两大类官方建议模型模型中名字的含义标题bert系列模型包含的文件bert系列模型参数参考链接bert的适合的场景 裸跑都非常优秀,句子级别(例如,SST-2)、句子对级别(例如MultiNLI)、单词级别(例…...

STM32G030C8T6:EEPROM读写实验(I2C通信)--M24C64

本专栏记录STM32开发各个功能的详细过程&#xff0c;方便自己后续查看&#xff0c;当然也供正在入门STM32单片机的兄弟们参考&#xff1b; 本小节的目标是&#xff0c;系统主频64 MHZ,采用高速外部晶振&#xff0c;实现PB11,PB10 引脚模拟I2C 时序&#xff0c;对M24C08 的EEPRO…...

opencascade 布尔运算笔记

BRepAlgoAPI_Common 对两个topods求解 没有公共部分也返回结果了 我想要的结果是没有公共部分返回false 在 Open CASCADE 中使用 BRepAlgoAPI_Common 进行布尔操作时&#xff0c;即使两个 TopoDS_Shape 没有公共部分&#xff0c;操作仍会返回一个结果。为了判断两个形状是否确…...

GPT-4o:人工智能新纪元的突破与展望

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…...

标准化、信息化、数字化、智能化、智慧化与数智化

近年来&#xff0c;标准化、信息化、数字化、智能化、智慧化以及数智化等词汇频繁出现&#xff0c;并逐渐成为业界热议的焦点。在国内&#xff0c;以华为、BAT等为代表的领军企业&#xff0c;不断强调“数字化转型”的重要性&#xff0c;并致力于推动其深入实施。与此同时&…...

14-JavaScript中的点操作符与方括号操作符

JavaScript中的点操作符与方括号操作符&#xff1a;简单理解与应用 笔记分享 在JavaScript中&#xff0c;访问对象的属性有两种常见方式&#xff1a;点操作符&#xff08;.&#xff09;和方括号操作符&#xff08;[]&#xff09;。尽管它们在很多情况下可以互换使用&#xff0…...

智慧大屏是如何实现数据可视化的?

智慧大屏&#xff0c;作为数据可视化的重要载体&#xff0c;已在城市管理、交通监控、商业运营等领域广泛应用。本文旨在阐述智慧大屏实现数据可视化的关键技术和方法&#xff0c;包括数据源管理、数据处理、视觉编码、用户界面与交互设计等。 大屏通过接入企业内部的数据库系…...

【JVM精通之路】垃圾回收-三色标记算法

首先预期你已经基本了解垃圾回收的相关知识&#xff0c;包括新生代垃圾回收器&#xff0c;老年代垃圾回收器&#xff0c;以及他们的算法&#xff0c;可达性分析等等。 先想象一个场景 最开始黑色节点是GC-Roots的根节点&#xff0c;这些对象有这样的特点因此被选为垃圾回收的根…...

Redis缓存(笔记一:缓存介绍和数据库启动)

目录 1、NoSQL数据库简介 2、Redis介绍 3、Redis(win系统、linux系统中操作) 3.1 win版本Redis启动 3.2 linux版本Redis启动 1、NoSQL数据库简介 技术的分类&#xff1a;&#xff08;发展史&#xff09; 1、解决功能性的问题&#xff1a;Java、Jsp、RDBMS、Tomcat、HTML、…...

OrangePi Kunpeng Pro套装测评:开箱与基本功能测试

前言 大家好&#xff0c;我是起个网名真难。非常荣幸受到香橙派的邀请&#xff0c;同时也是第一次做这个事情&#xff0c;很荣幸对香橙派与华为鲲鹏在2024年5月12日联合发布的新品——香橙派Kunpeng Pro开发板进行深入的评测。这款开发板是香橙派与华为鲲鹏合作推出的高性能平…...

RocketMQ教程(二):RocketMQ以及控制台的安装

RocketMQ-Console RocketMQ-Console 是一个针对 Apache RocketMQ 的开源 Web 监控和管理平台。它提供了一个用户友好的界面,通过 Web 浏览器允许用户对 RocketMQ 集群进行管理和监控。这个控制台使得管理和监测 RocketMQ 集群变得更加直观和方便,特别是对于不熟悉命令行操作的…...

电脑记事本怎么恢复之前的内容记录

每个人都曾有过这样的时刻——在记事本上精心记录下的重要内容&#xff0c;一不小心就被删除了。那种心情&#xff0c;仿佛一瞬间从山顶跌落到谷底&#xff0c;无尽的懊悔涌上心头。我也曾遭遇过这样的困境&#xff0c;那些消失的文字对我来说意义非凡&#xff0c;它们的丢失仿…...

多云管理“拦路虎”:深入解析网络互联、身份同步与成本可视化的技术复杂度​

一、引言&#xff1a;多云环境的技术复杂性本质​​ 企业采用多云策略已从技术选型升维至生存刚需。当业务系统分散部署在多个云平台时&#xff0c;​​基础设施的技术债呈现指数级积累​​。网络连接、身份认证、成本管理这三大核心挑战相互嵌套&#xff1a;跨云网络构建数据…...

测试微信模版消息推送

进入“开发接口管理”--“公众平台测试账号”&#xff0c;无需申请公众账号、可在测试账号中体验并测试微信公众平台所有高级接口。 获取access_token: 自定义模版消息&#xff1a; 关注测试号&#xff1a;扫二维码关注测试号。 发送模版消息&#xff1a; import requests da…...

OpenLayers 可视化之热力图

注&#xff1a;当前使用的是 ol 5.3.0 版本&#xff0c;天地图使用的key请到天地图官网申请&#xff0c;并替换为自己的key 热力图&#xff08;Heatmap&#xff09;又叫热点图&#xff0c;是一种通过特殊高亮显示事物密度分布、变化趋势的数据可视化技术。采用颜色的深浅来显示…...

Spark 之 入门讲解详细版(1)

1、简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室&#xff08;Algorithms, Machines, and People Lab&#xff09;开发通用内存并行计算框架。Spark在2013年6月进入Apache成为孵化项目&#xff0c;8个月后成为Apache顶级项目&#xff0c;速度之快足见过人之处&…...

黑马Mybatis

Mybatis 表现层&#xff1a;页面展示 业务层&#xff1a;逻辑处理 持久层&#xff1a;持久数据化保存 在这里插入图片描述 Mybatis快速入门 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6501c2109c4442118ceb6014725e48e4.png //logback.xml <?xml ver…...

《Qt C++ 与 OpenCV:解锁视频播放程序设计的奥秘》

引言:探索视频播放程序设计之旅 在当今数字化时代,多媒体应用已渗透到我们生活的方方面面,从日常的视频娱乐到专业的视频监控、视频会议系统,视频播放程序作为多媒体应用的核心组成部分,扮演着至关重要的角色。无论是在个人电脑、移动设备还是智能电视等平台上,用户都期望…...

跨链模式:多链互操作架构与性能扩展方案

跨链模式&#xff1a;多链互操作架构与性能扩展方案 ——构建下一代区块链互联网的技术基石 一、跨链架构的核心范式演进 1. 分层协议栈&#xff1a;模块化解耦设计 现代跨链系统采用分层协议栈实现灵活扩展&#xff08;H2Cross架构&#xff09;&#xff1a; 适配层&#xf…...

TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案

一、TRS收益互换的本质与业务逻辑 &#xff08;一&#xff09;概念解析 TRS&#xff08;Total Return Swap&#xff09;收益互换是一种金融衍生工具&#xff0c;指交易双方约定在未来一定期限内&#xff0c;基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...

数据库分批入库

今天在工作中&#xff0c;遇到一个问题&#xff0c;就是分批查询的时候&#xff0c;由于批次过大导致出现了一些问题&#xff0c;一下是问题描述和解决方案&#xff1a; 示例&#xff1a; // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...

Web 架构之 CDN 加速原理与落地实践

文章目录 一、思维导图二、正文内容&#xff08;一&#xff09;CDN 基础概念1. 定义2. 组成部分 &#xff08;二&#xff09;CDN 加速原理1. 请求路由2. 内容缓存3. 内容更新 &#xff08;三&#xff09;CDN 落地实践1. 选择 CDN 服务商2. 配置 CDN3. 集成到 Web 架构 &#xf…...