大三下每日打卡021

news/2025/3/26 3:00:56/文章来源:https://www.cnblogs.com/bzsc/p/18788680

实验二:UI设计

实验目的

本次实验的目的是让大家熟悉Android开发中的UI设计,包括了解和熟悉常用控件的使用、界面布局和事件处理等内容。

实验要求

  1. 熟悉和掌握界面控件设计
  2. 了解Android界面布局
  3. 掌握控件的事件处理

实验内容

一、常用控件

 

Android中有许多常用控件(简单分类):

文本框:TextView、EditText

按钮:Button、RadioButton、RadioGroup、CheckBox、ImageButton

列表:List、ExpandableListView、Spinner、AutoCompleteTextView、GridView、ImageView

进度条:ProgressBar、ProgressDialog、SeekBar、RatingBar

选择器:DatePicker、TimePicker

菜单:Menu、ContentMenu

对话框:Dialog、ProgressDialog

常用的控件有文本框、按钮和列表等。

 

EditText TextView 上的覆盖,将自身配置为可编辑。 它是 TextView 的预定义子类,包含丰富的编辑功能。我的理解就是一个输入框

 

 

 

 

 

2.控件的实现

TextView

  1. <TextView
  2. android:id="@+id/my_textView"
  3. android:layout_width="322dp"
  4. android:layout_height="75dp"
  5. android:layout_marginLeft="20dp"
  6. android:gravity="center_vertical"
  7. android:textSize="24sp"
  8. app:layout_constraintBottom_toBottomOf="parent"
  9. app:layout_constraintLeft_toLeftOf="parent"
  10. app:layout_constraintRight_toRightOf="parent"
  11. app:layout_constraintTop_toTopOf="parent" />

 

EditText

  1. <EditText
  2. android:id="@+id/myEditText"
  3. android:layout_width="match_parent"
  4. android:layout_height="100dp"
  5. android:layout_marginLeft="@dimen/fab_margin"
  6. android:layout_marginRight="@dimen/fab_margin"
  7. android:background="@drawable/editext_style"
  8. android:hint="请输入提款金额"
  9. android:ems="10"
  10. android:gravity="center|center_vertical"
  11. android:inputType="numberDecimal"
  12. android:textCursorDrawable="@android:color/black"
  13. android:textSize="36sp"
  14. android:textStyle="bold" />
  15. <Button
  16. android:id="@+id/myButton"
  17. android:layout_width="match_parent"
  18. android:layout_height="75dp"
  19. android:layout_marginLeft="@dimen/fab_margin"
  20. android:layout_marginTop="@dimen/fab_margin"
  21. android:layout_marginRight="@dimen/fab_margin"
  22. android:background="@drawable/button_style"
  23. android:enabled="false"
  24. android:text="提        现"
  25. android:textSize="24sp" />

Button

 

RadioButton

  1. <RadioButton
  2. android:id="@+id/rbMainCitySJZ"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:checked="false"
  6. android:text="石家庄"
  7. android:textSize="24sp" />
  8. <RadioGroup
  9. android:id="@+id/rgMainCity"
  10. android:layout_width="234dp"
  11. android:layout_height="127dp"
  12. app:layout_constraintBottom_toBottomOf="parent"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintHorizontal_bias="0.497"
  15. app:layout_constraintStart_toStartOf="parent"
  16. app:layout_constraintTop_toTopOf="parent"
  17. app:layout_constraintVertical_bias="0.241">
  18. 。。。。。。
  19. </RadioGroup>
  20. <CheckBox
  21. android:id="@+id/cbMainSJZ"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:checked="true"
  25. android:text="石家庄"
  26. android:textSize="24sp"
  27. app:layout_constraintBottom_toBottomOf="parent"
  28. app:layout_constraintEnd_toEndOf="parent"
  29. app:layout_constraintHorizontal_bias="0.449"
  30. app:layout_constraintStart_toStartOf="parent"
  31. app:layout_constraintTop_toTopOf="parent"
  32. app:layout_constraintVertical_bias="0.373" />
  33. <ImageButton
  34. android:id="@+id/ibtnMainOK"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. app:layout_constraintBottom_toBottomOf="parent"
  38. app:layout_constraintEnd_toEndOf="parent"
  39. app:layout_constraintHorizontal_bias="0.485"
  40. app:layout_constraintStart_toStartOf="parent"
  41. app:layout_constraintTop_toTopOf="parent"
  42. app:layout_constraintVertical_bias="0.764"
  43. app:srcCompat="@drawable/image_1" />

RadioGroup

CheckBox

ImageButton

 

 

 

 

 

 

 

3.控件的属性(放的是一些不熟悉的)

属性

作用

android:gravity="center|center_vertical"

用于设置视图内容在水平和垂直方向上的对齐方式

android:ems="10"

宽度为10个汉字

android:gravity="center|center_vertical"

用于设置视图内容在水平和垂直方向上的对齐方式

 

android:inputType="numberDecimal"

输入类型为数字类型

android:orientation="vertical"

把子视图垂直排列

android:textCursorDrawable="@android:color/black"

用于设置 EditText光标颜色

android:enabled="false"

某个组件是否被启用

android:checked="true"

用于设置可选中控件

app:layout_constraintBottom_toBottomOf="parent"

将视图的底部与父布局(parent)的底部对齐。

app:layout_constraintHorizontal_bias="0.498"

设置视图在水平方向上的偏移比例。取值范围:0 到 1。0:视图偏向左侧。1:视图偏向右侧。

app:layout_constraintLeft_toLeftOf="parent"

将视图的左侧与父布局(parent)的左侧对齐。

app:layout_constraintVertical_bias="0.181"

设置视图在垂直方向上的偏移比例。取值范围:0 到 1。0:视图偏向上1:视图偏向下

app:layout_constraintRight_toRightOf="parent"

将视图的右侧与父布局(parent)的右侧对齐。

app:layout_constraintTop_toTopOf="parent"

将视图的顶部与父布局(parent)的顶部对齐。

app:srcCompat="@drawable/image_1"

用于设置ImageView或ImageButton等视图的图像资源的属性

 

 

 

 

 

二、界面布局

LinearLayout布局

  1. <LinearLayout
  2. android:id="@+id/myLinearLayout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:layout_editor_absoluteX="196dp"
  7. tools:layout_editor_absoluteY="462dp">
  8. ....
  9. </LinearLayout>

 

 

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

线性布局的控件默认是从左往右排列或从上往下排列,如果想让线性布局中的控件排列对齐右边缘或者底部,可以用gravity属性控制。

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

 

三、事情处理

点击按钮后,Toast弹出按钮被触碰的事件信息

Toast.makeText(MainActivity.this,"click",Toast.LENGTH_SHORT).show();

 

 

View提供了多种事件监听器,用于处理用户与视图的交互。除了 setOnClickListenersetOnTouchListener  setOnKeyListener 之外,还有其他常用的监听器。

1.setOnClickListener

用途:处理点击事件。

myButton.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        // 处理点击事件

    }

});

2.setOnTouchListener

用途:处理触摸事件(按下、移动、抬起等)。

myButton.setOnTouchListener(new View.OnTouchListener() {

    @Override

    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                // 按下事件

                break;

            case MotionEvent.ACTION_MOVE:

                // 移动事件

                break;

            case MotionEvent.ACTION_UP:

                // 抬起事件

                break;

        }

        return false; // 返回 true 表示事件已处理,不再传递

    }

});

3.setOnKeyListener

用途:处理按键事件(如物理键盘或遥控器按键)

myButton.setOnKeyListener(new View.OnKeyListener() {

    @Override

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {

            // 处理回车键按下事件

            return true; // 返回 true 表示事件已处理

        }

        return false; // 返回 false 表示事件未处理

    }

});

 

4.setOnLongClickListener

用途:处理长按事件。

myButton.setOnLongClickListener(new View.OnLongClickListener() {

    @Override

    public boolean onLongClick(View v) {

        // 处理长按事件

        return true; // 返回 true 表示事件已处理

    }

});

5.setOnFocusChangeListener

用途:处理焦点变化事件(如视图获得或失去焦点)。

myButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {

    @Override

    public void onFocusChange(View v, boolean hasFocus) {

        if (hasFocus) {

            // 视图获得焦点

        } else {

            // 视图失去焦点

        }

    }

});

 

6.setOnDragListener

用途:处理拖拽事件。

myButton.setOnDragListener(new View.OnDragListener() {

    @Override

    public boolean onDrag(View v, DragEvent event) {

        switch (event.getAction()) {

            case DragEvent.ACTION_DRAG_STARTED:

                // 拖拽开始

                break;

            case DragEvent.ACTION_DRAG_ENDED:

                // 拖拽结束

                break;

            case DragEvent.ACTION_DROP:

                // 拖拽释放

                break;

        }

        return true; // 返回 true 表示事件已处理

    }

});

 

7. setOnCreateContextMenuListener

用途:处理上下文菜单创建事件(长按视图时弹出菜单)

myButton.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {

    @Override

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        // 添加上下文菜单项

        menu.add("菜单项1");

        menu.add("菜单项2");

    }

});

 

8. setOnSystemUiVisibilityChangeListener

用途:处理系统 UI 可见性变化事件(如状态栏或导航栏的显示/隐藏)。

myButton.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {

    @Override

    public void onSystemUiVisibilityChange(int visibility) {

        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {

            // 系统 UI(如状态栏)可见

        } else {

            // 系统 UI 隐藏

        }

    }

});

 

9. setOnGenericMotionListener

用途:处理通用运动事件(如鼠标或轨迹球事件)。

myButton.setOnGenericMotionListener(new View.OnGenericMotionListener() {

    @Override

    public boolean onGenericMotion(View v, MotionEvent event) {

        // 处理通用运动事件

        return true; // 返回 true 表示事件已处理

    }

});

 

10. setOnHoverListener

用途:处理悬停事件(如鼠标悬停)。

myButton.setOnHoverListener(new View.OnHoverListener() {

    @Override

    public boolean onHover(View v, MotionEvent event) {

        // 处理悬停事件

        return true; // 返回 true 表示事件已处理

    }

});

 

 

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

读DAMA数据管理知识体系指南29文件和内容管理活动

读DAMA数据管理知识体系指南29文件和内容管理活动1. 规划生命周期的管理 1.1. 从文件的创建或接收文件后的分发、存储、检索、归档和潜在的销毁 1.2. 规划包括开发分类/索引系统和分类法,以实现文件的存储和检索 1.3. 重要的是,生命周期规划中需要为档案建立具体的制度 1.4. …

c语言实验2

1 #include <stdio.h>2 #include <stdlib.h>3 #include <time.h>4 5 #define N 56 7 int main() {8 int number;9 int i; 10 11 srand(time(0)); // 以当前系统时间作为随机种子 12 for(i = 0; i < N; ++i) { 13 number = r…

使用 Browser-Use WebUI + DeepSeek 实现浏览器AI自动化全攻略

使用 Browser-Use WebUI + DeepSeek 实现浏览器AI自动化全攻略 环境准备 1. 安装 Python 环境版本要求:Python 3.11 或更高版本 验证安装:命令行执行 python --version 注意:安装时需勾选 "Add to PATH" 选项(Windows用户)2. 核心工具安装 # 安装 browser-use 框…

20234214 2024-2025-2 《Python程序设计》实验一报告

20234214 2024-2025-2 《Python程序设计》实验一报告 课程:《Python程序设计》 班级: 2342 姓名: 唐果儿 学号:20234214 实验教师:王志强 实验日期:2025年3月18日 必修/选修: 公选课 1.实验内容 (一)实验内容 1.熟悉Python开发环境; 2.练习Python运行、调试技能; …

WinForm 使用 Win32 API 实现的无边框窗口

WinForm 使用 Win32 API 实现的无边框窗口前言 时光荏苒,转眼已近是2025年了。不知不觉两年多没有研究代码了,在这期间 .NET 10 都快 RC 了,前几天刷手机看到张队公众号里有关于 .NET 9.0 AOT 发布的内容,所以写了这些代码来测试一下 AOT 编译的效果,并评估未来是否开发支…

C语言打卡学习第4天(2025.3.23)

今天只写了几道基础题,又看了下数组和冒泡排序,概念搞懂了但是写代码还是比较困难,准备明天把排序这类题好好看看。

一文速通Python并行计算:01 Python多线程编程-基本概念、切换流程、GIL锁机制和生产者与消费者模型

多线程允许程序同时执行多个任务,提升效率和响应性。线程分为新建、就绪、运行、阻塞和死亡五种状态。Python的GIL锁限制多线程并行执行,适合I/O密集型任务。生产者-消费者模型通过共享缓冲区和条件变量实现线程协作,解决数据共享问题。一文速通 Python 并行计算:01 Python…

Spring的三级缓存详解

目录 1、什么是三级缓存 2、三级缓存详解Bean实例化前属性赋值/注入前初始化后总结3、怎么解决的循环依赖 4、不用三级缓存不行吗 5、总结 一、什么是三级缓存 就是在Bean生成流程中保存Bean对象三种形态的三个Map集合,如下:

20244207 实验一 《python程序设计》实验报告

# 20244207 2024-2025-2 《Python程序设计》实验一报告 课程:《Python程序设计》 班级: 2442 姓名: 赵文萱 学号:20244207 实验教师:王志强 实验日期:2025年3月18日 必修/选修: 公选课 1.实验内容 1.熟悉Python开发环境; 2.练习Python运行、调试技能; 3.编写程序,…

20244202 《Python程序设计》实验一报告

20244202 《Python程序设计》实验一报告 课程:《Python程序设计》 班级: 2442 姓名: 陈艺豪 学号:20244219 实验教师:王志强 实验日期:2025年3月23日 必修/选修: 公选课 1.实验内容 (1).熟悉Python开发环境; (2).练习Python运行、调试技能; (3).编写程序,练习变量和类…

USTCPC 2025 游记

队名 合肥一中能不能多请点OI教练,二人队。队长 @包涵宇 ,省队爷。 Day -inf~0 随机写了几道有意思的 cf 。 bhy 又在做黑的插头 dp 。膜拜。 Day 1 早上被父母逼着学习文化课,结果作业做不完直接半红温状态,, 加上昨天做了 ~8h 文化课作业只完成了 1/2 ,然后直接不做了…