2、登录界面开发

news2024/9/21 5:31:47

【任务描述】

本任务要求使用线性布局以及TextView、EditText、Button等常见控件完成智慧园区登录界面的开发。

1、线性布局(LinearLayout

1.1、LinearLayout概述

线性布局(LinearLayout)主要以水平或垂直方式来排列界面中的控件。并将控件排列到一条直线上。在线性布局中,如果水平排列,垂直方向上只能放一个控件,如果垂直排列,水平方向上也只能方一个控件。

线性布局中的每个元素都位于前一个元素之后,但要注意的是,当元素排列到布局界面的边缘时,后面的元素将不会被显示出来。

1.2、LinearLayout常用属性

1、 orientation属性

在线性布局中,控件排列有水平和垂直两个方向,控件排列方向由android:orientation属性来控制,该属性需要加在LinearLayout标记的属性中。

从上图可以看出,将orientation属性值设置成为horizontal,控件将从水平方向从左往右排列,将orientation属性值设置成为vertical控件将从垂直方向从上往下排列。

2. layout_weight属性

LinearLayout中另外一个常用的属性是layout_weight,该属性需要加在LinearLayout的子控件中。其作用是分配线性布局中的剩余空间到该控件上。

当LinearLayout的排列方式是horizontal时,layout_weight只能设置宽度的比重,既然宽度使用layout_weight来设置,那么控件的宽度不再由layout_width来决定。同样的道理,当LinearLayout的排列方向是vertical时,layout_weight只能设置高度的比重,高度比重也不再由layout_height来决定。

2、TextView的使用

TextView(文本框)可以说是Android中最简单的一个控件了,改控件主要用于在界面上显示一段文本信息。开发者可以在代码中设置 TextView控件属性,如字体内容、大小,颜色、样式等。

下面是TextView常用属性

text = "文本"

textColor = "颜色值" #十六进制

textSize = "文字大小" 单位:sp(官方推荐)、dp、px

textStyle = "文字样式" 可选值:Bold(加粗)、italic(倾斜)、normal(无)

ellipsize = "文字超出截断" 可选值:end(后)、start(前)

maxLenght = "最大长度" 单位:Int,可容纳的最大字符数,超出隐藏

background = "颜色值/照片" #十六进制/图片路径,设置背景

Android中颜色代码用#ARGB表示,A代表透明度,R为Red,G为Green,B为Blue,其中每一个字母用都用一个十六进制来表示,其中透明度A从0到F表示完全透明到完全不透明。例如#FFFF就是完全不透明的白色(F为十六进制的最后一个,而红加绿加蓝自然就是白色了),表示完全不透明时,可以省略A,应用#RGB表示。大多数情况使用#AARRGGBB表示更高精度的颜色值(其中透明度AA可以省略)。

3、EditText(输入框)的使用

EditText控件也称输入框,它允许用户在控件里输入和编辑内容。EditTex常用属性:

1. hint:设置EditText中的提示内容

2. inputType:限制该文本框的输入内容,如textPassword表示输入的文本位密码类型,如number(限制为拨号建盘输入)等

4、Button的使用

Button控件,也就是按钮,是程序与用户交互非常重要的一个控件,起作用是相应用户的一系列点击事件。

常用属性

setClickable(boolean clickable)

设置按钮是否允许点击。clickable=true:允许点击。clickable=false:禁止点击

setBackgroundResource(int resid)

通过资源文件设置背景色。resid:资源xml 文件 ID。按钮默认背景为:android.R.drawable.btn_default

  • 使用匿名内部类的方式在Activity中直接实现Button的点击事件

  • 在Activity中实现View.onClickListener接口设置点击事件

xml文件

java文件

package com.example.firstapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.net.DatagramSocketImpl;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"点击了按钮1", Toast.LENGTH_LONG).show();
                Log.d(TAG, "onClick: 点击了");
            }
        });
    }
}

在Activity中实现View.onClickListener接口设置点击事件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2"/>
</LinearLayout>
package com.example.firstapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.net.DatagramSocketImpl;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn1,btn2;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
                Toast.makeText(this, "按钮1被点击了", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "onClick:1 ");
                break;
            case R.id.btn2:
                Toast.makeText(this, "按钮2被点击了", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "onClick: 2");
                break;
        }

    }
}

5、ImageView的使用

ImageView是Android开发中最常用的组件之一,主要用于显示图片,但是它不只是能显示图片,任何Drawable对象都可以使用它来显示。

1、ImageView常用属性

android:maxWidth:ImageView的最大宽度。

android:maxHeight:ImageView的最大高度。

android:adjustViewBounds:用于调整ImageView的边界,使得ImageView和图片有一样的长 宽比例,通常配合maxWidth、maxHeight一起使用。

app:srcCompat:设置要显示的Drawable对象的引用。

android:scaleType:设置图片的缩放类型

在学习这个控件之前,需要准备一些图片资源放置res/drawable目录下。

ImageView控件可以使用android:src属性把图片当作内容显示,也可以用android:background把图片当作背景显示。

通过例子看效果

6、RadioButton的使用

RadioButton(单选按钮)需要与RadioGroup(单选组合框)配合使用,才能实现多个单选按钮间的互斥效果。具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择性别"/>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男"/>
        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>
</LinearLayout>

7、CheckBox的使用

CheckBox(复选框),它允许用户一次选择多个选项。具体代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择爱好:"/>
    <CheckBox
        android:id="@+id/cb_basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球"/>
    <CheckBox
        android:id="@+id/cb_sing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="唱歌"/>
    <CheckBox
        android:id="@+id/cb_game"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="游戏"/>
    <CheckBox
        android:id="@+id/cb_tour"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旅游"/>
</LinearLayout>

8、边距的使用

在android的布局中,经常需要给控件和容器设置一些边距,如控件和控件之间的边距称为外边距,控件与其内容的边距称为内边距。

1.设置各个组件的外边距:

android:layout_marginTop

android:layout_marginBottom

android:layout_marginLeft

android:layout_marginRight

2.设置内边距

android:paddingBottom

android:paddingTop

android:paddingLeft

android:paddingRight

9、登录界面任务实施

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp"
    android:id="@+id/ll_main"
    tools:context=".MainActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textSize="20sp"
        android:hint="请输入账号"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textSize="20sp"
        android:inputType="textPassword"
        android:hint="请输入密码"
        />
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="记住密码"
        android:textSize="20sp"
        android:layout_marginLeft="5dp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textSize="20sp"
        android:text="登录"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textSize="20sp"
        android:text="注册"/>
</LinearLayout>

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

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

相关文章

【C++修炼之路】20.手撕红黑树

每一个不曾起舞的日子都是对生命的辜负 红黑树实现:RBTree 前言一.红黑树的概念及性质1.1 红黑树的概念1.2 红黑树的性质二.红黑树的结构2.1 红黑树节点的定义2.2 红黑树类的封装三.红黑树的插入情况1&#xff1a;只变色情况2&#xff1a;变色单旋情况3&#xff1a;双旋插入的代…

Docker入门和安装教程

一、Docker入门简介 Docker 是一个基于GO语言开发的开源的应用容器引擎&#xff0c;让开发者可以打包他们的应用以及依赖包到一个可移植的容器中&#xff0c;然后发布到任何流行的Linux机器上&#xff0c;也可以实现虚拟化。 容器是完全使用沙箱机制&#xff0c;相互之间不会…

关于如何在 Grafana 绘制 Apache Hudi Metrics 仪表盘的教程

文章目录前言下载 Prometheus下载 PushgatewayPrometheus 集成 PushgatewayPushgateway 后台执行Prometheus 后台执行在Prometheus中配置PushgatewayApache Hudi Metrics中开启 Pushgateway 推送Grafana 安装和启动Grafana 新增 Apache Hudi Metrics 仪表盘添加 Prometheus 数据…

批处理删除指定文件或文件夹

声明&#xff1a;1-2节参考了 https://blog.csdn.net/weixin_43960383/article/details/1243673841. DEL1.1 DEL 的命令参数使用 del 命令能指定文件&#xff0c;Del (erase)[Drive:][Path]FileName指删除指定文件。指定要删除的文件或文件集的位置和名称。语法格式如下&#x…

XML学习

文章目录XML什么是XML?XML的作用&#xff1f;XML语法文档声明XML注释元素&#xff08;标签&#xff09;xml属性语法规则5.4、xml解析技术介绍dom4j 解析技术&#xff08;重点&#xff09;dom4j 编程步骤&#xff1a;使用遍历标签 获取所有标签中的内容&#xff08;重点&#x…

第十三届蓝桥杯国赛 C++ C 组 Java A 组 C 组 Python C 组 E 题——斐波那契数组(三语言代码AC)

目录1.斐波那契数组1.题目描述2.输入格式3.输出格式4.样例输入5.样例输出6.数据范围7.原题链接2.解题思路3.Ac_code1.Java2.C3.Python1.斐波那契数组 1.题目描述 如果数组 A(a0,a1,⋯.an−1)A(a_0,a_1,⋯.a_{n-1})A(a0​,a1​,⋯.an−1​)满足以下条件, 就说它是一个斐波那契…

如何在类中定义构造方法?

在一个类中定义的方法如果同时满足以下三个条件&#xff0c;该方法称为构造方法&#xff0c;具体如下&#xff1a;1、方法名与类名相同2、在方法名的前面没有返回值类型的声明3、在方法中不能使用return语句返回一个值接下来通过一个案例来演示如何在类中定义构造方法&#xff…

闪光桐人の实习日记(2023年2月20-27日)

前往闪闪の小窝以获得更好的阅读和评论体验 文章目录2023年2月20日&#xff08;Vue入门&#xff09;概念Vue基础Vue中的MVVMVue的体验Vue的生命周期Vue指令Vue组件VueRouter前后端路由的区别工作原理两种模式比较route跟router的区别路由属性导航守卫Vuex概述5种基本对象基本使…

C语言编程规范 第二部分

2、头文件对于C语言来说&#xff0c;头文件的设计体现了大部分的系统设计。不合理的头文件布局是编译时间过长的根因&#xff0c;不合理的头文件实际上反映了不合理的设计。 1、头文件中适合放置接口的声明&#xff0c;不适合放置实现头文件是模块&#xff08;Module&#xff…

【数据结构】时间复杂度和空间复杂度

&#x1f307;个人主页&#xff1a;平凡的小苏 &#x1f4da;学习格言&#xff1a;别人可以拷贝我的模式&#xff0c;但不能拷贝我不断往前的激情 &#x1f6f8;C语言专栏&#xff1a;https://blog.csdn.net/vhhhbb/category_12174730.html 小苏希望大家能从这篇文章中收获到许…

苹果手机怎么设置闹钟铃声?更改为歌曲铃声,亲测有效

很不是有很多小伙伴每天早上都被苹果手机刺耳的“雷达”闹钟铃声给吵醒呢&#xff1f;想要更换一个舒缓的闹钟铃声&#xff0c;却发现自己鼓捣半天却无法更换喜欢的歌曲闹钟铃声。苹果手机怎么设置闹钟铃声&#xff1f;下面小编就来分享如何将苹果手机的闹钟铃声设置成歌曲铃声…

【docker】拉取镜像环境报错解决#ERROR: Get https://registry-1.docker.io/v2/

&#x1f341;博主简介   &#x1f3c5;云计算领域优质创作者   &#x1f3c5;华为云开发者社区专家博主   &#x1f3c5;阿里云开发者社区专家博主 &#x1f48a;交流社区&#xff1a;运维交流社区 欢迎大家的加入&#xff01; 文章目录问题报错原因解决方法问题 ERROR…

简单的认识 Vue(vue-cli安装、node安装、开发者工具)

Vue1、Vue 与其他框架的对比及特点1.1 Vue.js 是什么1.2 作者1.3 作用1.4 Vue 与其他框架的对比2、安装 Vue 的方法2.1 CDN 引入2.2 脚手架工具2.3 vue 开发者工具安装3、创建第一个实例4、理解 Vue 的 MVVM 模式5、数据双向绑定5.1 感受响应式实验总结1、Vue 与其他框架的对比…

Flutter WebView 如何与 h5 同步登录状态

大家好&#xff0c;我是 17。 Flutter WebView 一共三篇文章 在 Flutter 中使用 webview_flutter 4.0 | js 交互Flutter WebView 性能优化&#xff0c;让 h5 像原生页面一样优秀Flutter WebView 如何与 h5 同步登录状态 本篇是第 3 篇&#xff0c;讲下 Flutter WebView 与 h…

Python|每日一练|动态规划|图算法|散列表|数组|双指针|单选记录:不同路径|求两个给定正整数的最大公约数和最小公倍数|删除有序数组中的重复项

1、不同路径&#xff08;数学&#xff0c;动态规划&#xff09; 一个机器人位于一个 m x n 网格的左上角 &#xff08;起始点在下图中标记为 “Start” &#xff09;。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角&#xff08;在下图中标记为 “Finish”…

【大数据基础】Linux常用命令

参考资料&#xff1a; https://www.runoob.com/w3cnote/linux-common-command-2.html 1、ls命令 就是 list 的缩写&#xff0c;通过 ls 命令不仅可以查看 linux 文件夹包含的文件&#xff0c;而且可以查看文件权限(包括目录、文件夹、文件权限)查看目录信息等等。 ls -a 列…

LockSupport常用方法源码分析

前言&#xff1a;本文将介绍LockSupport类中的方法和部分源码&#xff0c;以及面试常问到的一个小问题&#xff0c;感兴趣的大佬可以指点下。 希望能够加深自己的印象以及帮助到其他的小伙伴儿们&#x1f609;&#x1f609;。 如果文章有什么需要改进的地方还请大佬不吝赐教&am…

uniapp项目引入vant2遇到报错Uncaught ReferenceError: require is not defined完美解决方案

一、问题描述 我用的是Vue2版本的uniapp项目&#xff0c;以下是Vant官方提供的安装方法&#xff0c;使用npm安装到uniapp项目中。 Vant官网&#xff1a;https://vant-contrib.gitee.io/vant/v2/#/zh-CN/quickstart 安装完成得到以下模块 官方提供的引入单个组件的方案 我需要引…

Vision Transformer(ViT) 2: 应用及代码讲解

文章目录1. 代码讲解1.1 PatchEmbed类1&#xff09;__init__ 函数2) forward 过程1.2 Attention类1&#xff09;__init__ 函数2&#xff09;forward 过程1.3 MLP类1&#xff09;__init__ 函数2&#xff09;forward函数1.4 Block类1&#xff09;__init__ 函数2&#xff09;forwa…

MATLAB曲线拟合工具箱

MATLAB曲线拟合工具箱一、MATLAB曲线拟合工具箱1.导出拟合后的曲线数据2.调用m文件中的函数3.显示5位有效数字二、参考链接一、MATLAB曲线拟合工具箱 1.导出拟合后的曲线数据 生成代码后&#xff0c;默认函数名为createFit&#xff0c;可以自行修改&#xff0c;直接保存&#…