android.widget包介绍

devtools/2025/3/18 18:47:12/

android.widget 是 Android 开发中用于构建用户界面(UI)的核心包之一。它包含了大量的 UI 控件(如按钮、文本框、列表等),这些控件通常被称为“小部件”(Widget)。android.widget 包中的类大多继承自 android.view.View,因此它们都具备视图的基本特性。

以下是 android.widget 包中一些常用类的详细介绍:


1. 常用控件

(1) Button
  • 用于触发用户交互的按钮。
  • 继承自 TextView
  • 常用属性
    • android:text:设置按钮文本。
    • android:onClick:指定点击事件的处理方法。
  • 示例
    import android.widget.Button;Button button = findViewById(R.id.button);
    button.setOnClickListener(v -> {Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show();
    });
    
(2) TextView
  • 用于显示文本内容。
  • 常用属性
    • android:text:设置显示的文本。
    • android:textSize:设置文本大小。
    • android:textColor:设置文本颜色。
  • 示例
    import android.widget.TextView;TextView textView = findViewById(R.id.textView);
    textView.setText("Hello, World!");
    
(3) EditText
  • 用于接收用户输入的文本。
  • 继承自 TextView
  • 常用属性
    • android:hint:设置提示文本。
    • android:inputType:设置输入类型(如文本、数字、密码等)。
  • 示例
    import android.widget.EditText;EditText editText = findViewById(R.id.editText);
    String input = editText.getText().toString();
    
(4) ImageView
  • 用于显示图片。
  • 常用属性
    • android:src:设置图片资源。
    • android:scaleType:设置图片的缩放类型。
  • 示例
    import android.widget.ImageView;ImageView imageView = findViewById(R.id.imageView);
    imageView.setImageResource(R.drawable.my_image);
    
(5) CheckBox
  • 用于多选框。
  • 常用属性
    • android:checked:设置默认选中状态。
  • 示例
    import android.widget.CheckBox;CheckBox checkBox = findViewById(R.id.checkBox);
    if (checkBox.isChecked()) {// 复选框被选中
    }
    
(6) RadioButtonRadioGroup
  • 用于单选按钮组。
  • 常用属性
    • android:checkedButton:设置默认选中的单选按钮。
  • 示例
    import android.widget.RadioGroup;
    import android.widget.RadioButton;RadioGroup radioGroup = findViewById(R.id.radioGroup);
    radioGroup.setOnCheckedChangeListener((group, checkedId) -> {RadioButton radioButton = findViewById(checkedId);Toast.makeText(this, radioButton.getText(), Toast.LENGTH_SHORT).show();
    });
    
(7) Spinner
  • 用于下拉选择框。
  • 常用属性
    • android:entries:设置下拉选项。
  • 示例
    import android.widget.Spinner;Spinner spinner = findViewById(R.id.spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    
(8) ProgressBar
  • 用于显示进度。
  • 常用属性
    • android:progress:设置当前进度。
    • android:max:设置最大进度值。
  • 示例
    import android.widget.ProgressBar;ProgressBar progressBar = findViewById(R.id.progressBar);
    progressBar.setProgress(50); // 设置进度
    
(9) Switch
  • 用于开关控件。
  • 常用属性
    • android:checked:设置默认开关状态。
  • 示例
    import android.widget.Switch;Switch switchButton = findViewById(R.id.switchButton);
    if (switchButton.isChecked()) {// 开关打开
    }
    

2. 布局控件

android.widget 包中还有一些用于布局的控件,例如:

(1) LinearLayout
  • 线性布局,可以水平或垂直排列子视图。
  • 常用属性
    • android:orientation:设置排列方向(verticalhorizontal)。
  • 示例
    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2" />
    </LinearLayout>
    
(2) RelativeLayout
  • 相对布局,子视图可以相对于父视图或其他子视图定位。
  • 常用属性
    • android:layout_alignParentTop:与父视图顶部对齐。
    • android:layout_toRightOf:位于指定视图的右侧。
  • 示例
    <RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2"android:layout_toRightOf="@id/button1" />
    </RelativeLayout>
    
(3) FrameLayout
  • 帧布局,子视图可以叠加显示。
  • 示例
    <FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/image1" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Overlay Text" />
    </FrameLayout>
    

3. 适配器控件

android.widget 包中还有一些用于显示列表或网格的控件,例如:

(1) ListView
  • 用于显示垂直滚动的列表。
  • 常用属性
    • android:entries:设置列表项。
  • 示例
    import android.widget.ListView;ListView listView = findViewById(R.id.listView);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
    listView.setAdapter(adapter);
    
(2) GridView
  • 用于显示网格布局。
  • 常用属性
    • android:numColumns:设置列数。
  • 示例
    GridView gridView = findViewById(R.id.gridView);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
    gridView.setAdapter(adapter);
    

4. 总结

android.widget 包提供了丰富的 UI 控件,涵盖了从按钮、文本框到列表、网格等各种常用组件。以下是常见的应用场景:

  • 按钮和文本:使用 ButtonTextViewEditText 等控件实现用户交互。
  • 列表和网格:使用 ListViewGridView 显示数据。
  • 布局管理:使用 LinearLayoutRelativeLayoutFrameLayout 等布局控件组织界面。

http://www.ppmy.cn/devtools/168129.html

相关文章

求职招聘网站源码,找工作招工系统,支持H5和各种小程序

招聘找活招工平台系统源码 招聘求职找工作软件 发布信息积分充值招聘系统,里面带纤细教程 功能介绍: 招工小程序主要针对工地招工工人找工作,工地可以发布招工信息,工人可以发布找活信息,招工信息可以置顶,置顶需要积分,积分可以通过签到、分享邀请好友、充值获取,后…

解决diffusers加载stablediffusion模型,输入prompt总是报错token数超出clip最大长度限制

1. StableDiffusion1.5 在加载huggingface中的扩散模型时&#xff0c;输入prompt总是会被报错超过clip的最大长度限制。 解决方案&#xff1a;使用compel库 from diffusers import AutoPipelineForText2Image import torch import pdb from compel import Compeldevice torc…

浅谈StarRocks SQL性能检查与调优

StarRocks性能受数据建模、查询设计及资源配置核心影响。分桶键选择直接决定数据分布与Shuffle效率&#xff0c;物化视图可预计算复杂逻辑。执行计划需关注分区裁剪、谓词下推及Join策略&#xff0c;避免全表扫描或数据倾斜。资源层面&#xff0c;需平衡并行度、内存限制与网络…

springboot441-基于SpringBoot的校园自助交易系统(源码+数据库+纯前后端分离+部署讲解等)

&#x1f495;&#x1f495;作者&#xff1a; 爱笑学姐 &#x1f495;&#x1f495;个人简介&#xff1a;十年Java&#xff0c;Python美女程序员一枚&#xff0c;精通计算机专业前后端各类框架。 &#x1f495;&#x1f495;各类成品Java毕设 。javaweb&#xff0c;ssm&#xf…

【通缩螺旋的深度解析与科技破局路径】

通缩螺旋的深度解析与科技破局路径 一、通缩螺旋的形成机制与恶性循环 通缩螺旋&#xff08;Deflationary Spiral&#xff09;是经济学中描述价格持续下跌与经济衰退相互强化的动态过程&#xff0c;其核心逻辑可拆解为以下链条&#xff1a; 需求端萎缩&#xff1a;居民消费信…

Excel导出工具类--复杂的excel功能导出(使用自定义注解导出)

Excel导出工具类 前言: 简单的excel导出,可以用easy-excel, fast-excel, auto-poi,在导出实体类上加上对应的注解,用封装好的工具类直接导出,但对于复杂的场景, 封装的工具类解决不了,要用原生的excel导出(easy-excel, fast-excel, auto-poi都支持原生的) 业务场景: 根据…

python爬虫碰到IP被封的情况,如何解决?

在数据抓取和爬虫开发的实践中&#xff0c;Python作为一种功能强大且易于上手的编程语言&#xff0c;被广泛应用于网络数据的采集。然而&#xff0c;随着网络环境的日益复杂&#xff0c;爬虫活动也面临着越来越多的挑战&#xff0c;其中IP被封便是常见且棘手的问题。IP被封不仅…

【春招笔试】2025.03.13-蚂蚁春招笔试题

题目总结 题目一:区间未出现的最小值之和 1️⃣:统计全为1的子数组数量和全为0的子数组数量,利用公式计算 2️⃣:利用数学公式 n(n+1) - 2N0 - N1 计算最终答案 难度:中等 这道题目的关键在于理解 mex 的概念,并发现对于只含 0 和 1 的数组,mex 值只可能是 0、1 或 2。…