Android-登录注册页面(第三次作业)

news2024/9/24 4:21:17

第三次作业 - 登录注册页面

题目要求

嵌套布局。使用线性布局的嵌套结构,实现登录注册的页面。(例4-3)

创建空的Activity

项目结构树如下图所示:

image-20231027223632461

注意:MainActivity.java文件并为有任何操作,主要功能集中在LoginActivity和SignUpActivity两个Activity中。

创建LoginActivity和SignUpAcivity

  1. 创建Activity

image-20231027224556713

  1. 创建LoginActivity和SignUpActivity

image-20231027224927233

修改AndroidManifest.xml文件,注释掉MainActivity的隐式启动代码

image-20231028091904882

  1. values文件夹中string.xml和color.xml修改

    • color.xml(添加blue代码)

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <color name="purple_200">#FFBB86FC</color>
          <color name="purple_500">#FF6200EE</color>
          <color name="purple_700">#FF3700B3</color>
          <color name="teal_200">#FF03DAC5</color>
          <color name="teal_700">#FF018786</color>
          <color name="black">#FF000000</color>
          <color name="white">#FFFFFFFF</color> 
          <color name="blue">#FF7BBAF7</color> 
      </resources>
      
    • string.xml(添加如下代码)

      <string name="academic_prompt">请选择学历</string>
      <string-array name="academic" >
          <item>博士</item>
          <item>硕⼠</item>
          <item>大学</item>
          <item>高中</item>
      </string-array>
      

      image-20231027225843441

  2. 自定义按钮样式布局文件,并且命名为btn_press_blue

    • 如何创建自定义样式布局文件

      image-20231027225307809

    • 添加代码

      image-20231027225952119

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">     <!--按压-->
            <shape>
                <solid android:color="#0082FF"/>
                <corners android:radius="10dp"/>
            </shape>
        </item>
    
        <item android:state_pressed="false">
            <shape>
                <solid android:color="@color/blue"/>
                <corners android:radius="10dp"/>
            </shape>
        </item>
    </selector>
    

修改LoginActivit和SignUpActivity的布局文件

  • activity_login.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:paddingRight="16dp"
        android:paddingLeft="16dp">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="欢迎选择DIY"
            android:textSize="20sp"
            android:layout_centerHorizontal="true"
            />
    
        <!--设置用户栏-->
        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="80dp"
            android:hint="用户名"
            android:textSize="20sp"
            android:textColor="#FFAD33"
            android:maxLines="1" />
        <!--密码栏-->
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/username"
            android:layout_marginTop="40dp"
            android:hint="密码"
            android:inputType="textPassword"
            android:textSize="20sp"
            android:textColor="#FFAD33"
            android:maxLines="1"/>
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/password"
            android:layout_marginTop="80dp">
            <Button
                android:id="@+id/btnLogin"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:background="@drawable/btn_press_blue"
                android:text="登录"
                android:textColor="#FFFFFF"/>
    
            <Button
                android:id="@+id/btnRegister"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:background="@drawable/btn_press_blue"
                android:text="注册"
                android:textColor="#FFFFFF"/>
    
        </LinearLayout>
    
    
    </RelativeLayout>
    

    image-20231027230427916

  • activity_sign_up.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:paddingRight="16dp"
        android:paddingLeft="16dp">
    
        <TextView
            android:id="@+id/signup_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            android:textSize="25sp"
            android:layout_margin="25dp"
            android:layout_centerHorizontal="true"/>
    
        <EditText
            android:id="@+id/UserName_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/signup_msg"
            android:singleLine="true"
            android:hint="用户名"/>
    
    
        <EditText
            android:id="@+id/PassWord_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/UserName_msg"
            android:singleLine="true"
            android:hint="密码"/>
    
        <EditText
            android:id="@+id/RPassWord_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/PassWord_msg"
            android:singleLine="true"
            android:hint="确认密码"/>
    
    <!--性别-->
            <TextView
                android:id="@+id/sex_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/RPassWord_msg"
                android:layout_marginTop="10dp"
                android:textSize="20sp"
                android:text="性别:"/>
    
            <RadioGroup
                android:id="@+id/rg_sex"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/RPassWord_msg"
                android:layout_toRightOf="@+id/sex_msg"
                android:layout_marginTop="10dp"
                android:orientation="horizontal">
                <RadioButton
                    android:id="@+id/sex_male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textSize="20sp"
                    android:checked="true"/>
    
                <RadioButton
                    android:id="@+id/sex_female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textSize="20sp"/>
            </RadioGroup>
    
    
    <!--    学历-->
        <TextView
            android:id="@+id/academic_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学历:"
            android:textSize="20sp"
            android:layout_below="@+id/rg_sex"
            android:layout_marginTop="20dp"/>
    
        <Spinner
            android:id="@+id/academic_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:prompt="@string/academic_prompt"
            android:entries="@array/academic"
            android:spinnerMode="dialog"
            android:layout_below="@+id/rg_sex"
            android:layout_toRightOf="@+id/academic_text"
            android:layout_toEndOf="@id/academic_text"
            android:fadeScrollbars="true"
            android:scrollIndicators="right"
            android:textSize="20sp"/>
    
        <LinearLayout
            android:layout_marginTop="20dp"
            android:id="@+id/hobby_msg"
            android:layout_below="@+id/academic_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="爱好:"
                android:textSize="20sp"/>
    
            <CheckBox
                android:id="@+id/hobby_swim"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="游泳"
                android:textSize="20sp"/>
    
            <CheckBox
                android:id="@+id/hobby_music"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="音乐"
                android:textSize="20sp"/>
    
            <CheckBox
                android:id="@+id/hobby_book"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="读书"
                android:textSize="20sp"/>
    
        </LinearLayout>
    
        <Button
            android:id="@+id/btn_RegisterPlus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/hobby_msg"
            android:layout_marginTop="20dp"
            android:layout_centerHorizontal="true"
            android:text="注册"
            android:background="@drawable/btn_press_blue"
            android:onClick="onRegClick"/>
    
    
    </RelativeLayout>
    

    image-20231027230405347

这里我们看到布局文件并不是我们之前在color.xml预设的blue(蓝色)的颜色,修改values/themes/themes.xml文件内容即可。

image-20231027230623090

设置页面跳转和按钮的监听事件

LoginActivit.java

public class LoginActivity extends AppCompatActivity {
    private Button btnLogin, btnRegister;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //去掉标题行
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_login);

        btnLogin = findViewById(R.id.btnLogin);
        btnRegister = findViewById(R.id.btnRegister);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
                startActivity(intent);
            }
        });
    }
}

SignUpActivity.java

public class SignUpActivity extends AppCompatActivity {

    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        spinner = findViewById(R.id.academic_msg);
    }
    public void onRegClick(View view){
        Toast.makeText(this,spinner.getSelectedItem().toString(),Toast.LENGTH_SHORT).show();
    }
}

启动项目

最后启动项目,可能会报错,大概率是下面的错误:

image-20231027231316699

修改代码即可

image-20231027231419210

修改后点击Sync Now更新

image-20231027231508660

更新完毕后如下图所示:

image-20231027231548275

效果展示

image-20231028092144699

点击登录按钮,跳转到MainActivity页面,点击注册页面,跳转到注册页面,选择学历后,点击注册按钮后,Toast弹出显示你选择的学历。

如果大家在这个过程中遇到了问题,可以在评论区或者私信我✌️

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1143165.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Android中 BufferQueue 和 Gralloc

目录 零、本篇讨论范围一、图片数据流的生产者与消费者1.1 生产者1.2 消费者 二、生产者与消费者间数据的传递2.1 BufferQueue2.2 Gralloc 零、本篇讨论范围 接上篇 SurfaceFlinger做Layer合成时&#xff0c;如何与HAL层进行交互 后&#xff1a; 本篇的讨论范围如下图红框中所…

selenium 根据【关键词】获取知网文献信息

哈喽大家好&#xff0c;我是咸鱼 之前咸鱼写过几篇关于知网爬虫的文章&#xff0c;后台反响都很不错。虽然但是&#xff0c;咸鱼还是忍不住想诉苦一下 有些小伙伴文章甚至代码看都没看完&#xff0c;就问我 ”为什么只能爬这么多条文献信息&#xff1f;“&#xff08;看过代码…

Python Selenium 之数据驱动测试的实现!

数据驱动模式的测试好处相比普通模式的测试就显而易见了吧&#xff01;使用数据驱动的模式&#xff0c;可以根据业务分解测试数据&#xff0c;只需定义变量&#xff0c;使用外部或者自定义的数据使其参数化&#xff0c;从而避免了使用之前测试脚本中固定的数据。可以将测试脚本…

从瀑布模式到水母模式:ChatGPT如何赋能软件研发全流程

文章目录 前言内容简介作者简介专家推荐读者对象直播预告 前言 计算机技术的发展和互联网的普及&#xff0c;使信息处理和传输变得更加高效&#xff0c;极大地改变了金融、商业、教育、娱乐等领域的运作方式。数据分析、人工智能和云计算等新兴技术&#xff0c;也在不断地影响和…

影响光源的因素

影响光源的因素 对比度 1.对比度 均匀性 2.均匀性 色彩还原性 3.色彩还原性 其他因素&#xff1a; 4. 亮度 &#xff1a; 光源 亮度是光源选择时的重要参考&#xff0c;尽量选择亮度高的光源。 5. 鲁棒性 &#xff1a; 鲁棒性是指光源是否对部件的位置敏感度最小 。 6. 光…

Leetcode 剑指 Offer II 050. 路径总和 III

题目难度: 中等 原题链接 今天继续更新 Leetcode 的剑指 Offer&#xff08;专项突击版&#xff09;系列, 大家在公众号 算法精选 里回复 剑指offer2 就能看到该系列当前连载的所有文章了, 记得关注哦~ 题目描述 给定一个二叉树的根节点 root &#xff0c;和一个整数 targetSum…

大厂面试题-什么是内存溢出,什么是内存泄漏?

目录 1、什么是内存溢出&#xff1f; 2、什么是内存泄漏&#xff1f; 3、如何避免&#xff1f; 1、什么是内存溢出&#xff1f; 我们来看到右侧的区域&#xff0c;假设我们JVM中可用的内存空间只剩下3M&#xff0c;但是我们要创建一个5M的对象&#xff0c;那么&#xff0c;…

前端JS for循环内异步接口变成同步提交(JavaScript for循环异步变同步)

遇见的问题&#xff1a; 导入Excel文件的时候&#xff0c;将每行数据整合成一个数组&#xff0c;循环数组插入每一条数据&#xff0c;插入数据后要判断是否插入成功&#xff0c;如果没插入成功的话&#xff0c;停止循环&#xff0c;不再插入后面的数据。甚至插入数据后&#xf…

【Leetcode】反转单链表

反转单链表 反转单链表题目题目思路代码 反转单链表题目 给你单链表的头节点 head &#xff0c;请你反转链表&#xff0c;并返回反转后的链表。 题目思路 链表的本质就是改变每一个结点的next域。 我们从第一个结点开始遍历&#xff0c;改变它的next域。 当我们要注意在改变…

尚未解决:use_python()和use_virtualenv()的使用

reticulate包为Python和R之间的互操作性提供了一套全面的工具。该包包含以下功能&#xff1a; 以多种方式从R调用Python&#xff0c;包括RMarkdown、获取Python脚本、导入Python模块以及在R会话中交互使用Python。 R和Python对象之间的转换&#xff08;例如&#xff0c;R和Pan…

TP项目启用websocket聊天功能 - gateway、wss、swoole、长连接 - PHP

TP项目启用websocket聊天功能 须知 swoole不支持windows安装,没有windows扩展WebSocket 在线测试(可测本地wss连接) websocket在线测试建议gateway只负责给终端发信,不参与逻辑部分后台负责所有的收信+发信安排,可以方便地获取用户好友关系、上下线状态管理、消息缓存、已…

图文并茂的帮助文档你值得拥有

概述 工作中除了写代码开发需求&#xff0c;也需要写文档&#xff0c;怎么写好一个文档能够让读者既能看懂API&#xff0c;又能快速上述操作&#xff0c;所见即所得。本文基于vitepress、ace-builds带大家实现一个这样好用的帮助文档。 实现效果 在线预览地址&#xff1a;ht…

4.5 数据加密

思维导图&#xff1a; 4.5 数据加密 为确保高度敏感数据的安全性&#xff0c;如财务、军事及国家机密数据&#xff0c;可采用数据加密技术。此技术将原始数据&#xff08;明文&#xff09;转化为不可识别格式&#xff08;密文&#xff09;&#xff0c;确保不知解密方法的人无法…

提高车联远控异常分析效率的设想

提高车联远控异常分析效率的设想 前言 随着汽车集成度、智能化、软件功能越来越丰富&#xff0c;用户车辆使用已不是传统的出行、驾驶等物理场景&#xff0c;更多的人与车的互动功能的场景。其中车联远控功能使用日益增多。技术人员开展排查车联远控问题时&#xff0c;往往需…

图解kd树+Python实现

开篇 在讲解k-近邻算法的时候&#xff0c;我们提供的思路是&#xff1a;对于新到来的样本&#xff0c;计算该样本与训练集中所有样本之间的距离&#xff0c;选取训练集中距离新样本最近的k个样本中大多数样本的类别作为新的样本的类别。 也就是说&#xff0c;每次都要计算新的样…

c语言基础:L1-060 心理阴影面积

这是一幅心理阴影面积图。我们都以为自己可以匀速前进&#xff08;图中蓝色直线&#xff09;&#xff0c;而拖延症晚期的我们往往执行的是最后时刻的疯狂赶工&#xff08;图中的红色折线&#xff09;。由红、蓝线围出的面积&#xff0c;就是我们在做作业时的心理阴影面积。 现给…

4.6 其他安全性保护

思维导图&#xff1a; 4.6 其他安全性保护 1. 推理控制 (Inference Control) 定义&#xff1a;处理强制存取控制未解决的问题&#xff0c;如利用列的函数依赖关系&#xff0c;从低安全等级信息推导出高安全等级信息。示例&#xff1a;在公司信息系统中&#xff0c;姓名和职务为…

SQL查询优化---如何查询截取分析

慢查询日志 1、慢查询日志是什么 MySQL的慢查询日志是MySQL提供的一种日志记录&#xff0c;它用来记录在MySQL中响应时间超过阀值的语句&#xff0c;具体指运行时间超过long_query_time值的SQL&#xff0c;则会被记录到慢查询日志中。 具体指运行时间超过long_query_time值的…

use renv with this project create a git repository

目录 1-create a git repository 2-Use renv with this project 今天在使用Rstudio过程中&#xff0c;发现有下面两个新选项&#xff08;1&#xff09;create a git repository (2) Use renv with this project. 选中这两个选项后&#xff0c;创建新项目&#xff0c;在项目目…

Redis(01)| 数据结构

这里写自定义目录标题 Redis 速度快的原因除了它是内存数据库&#xff0c;使得所有的操作都在内存上进行之外&#xff0c;还有一个重要因素&#xff0c;它实现的数据结构&#xff0c;使得我们对数据进行增删查改操作时&#xff0c;Redis 能高效的处理。 因此&#xff0c;这次我…