android.widget包介绍

ops/2025/3/18 21:14:24/

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/ops/166865.html

相关文章

windows协议不再续签,华为再无windows可用,将于四月发布鸿蒙PC

大家好&#xff0c;我是国货系创始人张云泽&#xff0c;最近不少小伙伴在后台问&#xff1a;“听说Windows协议要到期了&#xff1f;我的电脑会不会变砖&#xff1f;”还有人说&#xff1a;“华为笔记本以后用不了Windows了&#xff1f;鸿蒙系统能用吗&#xff1f;”今天咱们就…

todolist docker 小工具

参考链接 前排提示 没有中文&#xff0c;可使用浏览器 翻译 前提 安装docker安装docker-compose 下载仓库 git clone https://github.com/JordanKnott/taskcafe进行安装 cd taskcafe docker-compose -p taskcafe up -d服务启动后会监听在 3333 端口上&#xff0c;通过浏览器…

go数据结构笔记

常用数据结构 切片 定义int类型&#xff0c;大小为3 长度为10 切片的数据结构可以类比成ArrayList&#xff0c;具备动态扩容的数组 package mainimport "fmt"func main() {var numbers make([]int, 3, 10) //切片类型为int 长度为3 容量为10printSlice(numbers)…

MariaDB 10.6.21(安装后实际版本为10.6.19)

确保Mysql卸载干净&#xff01; 卸载自带的 MariaDB 查看版本&#xff1a;有则需要卸载。 rpm -qa|grep mariadb卸载&#xff1a;复制文件名&#xff0c;执行以下指令。 rpm -e --nodeps 文件名确认卸载&#xff1a; rpm -qa|grep mariadb 添加 MariaDB yum 仓库 首先在Cen…

Java Stream API 的使用

java8引入的java.util.stream.Stream流操作&#xff0c;使得访问和操作数组&#xff08;Array&#xff09;、集合&#xff08;Collection&#xff09;变得非常方便和优雅。 1、过滤元素和转化元素类型 private static void filterMapToInt() {List<String> list new Arr…

五子棋小游戏-简单开发版

一、需求分析 开发一个基于 Pygame 库的五子棋小游戏&#xff0c;允许两名玩家在棋盘上轮流落子&#xff0c;当有一方达成五子连珠时游戏结束&#xff0c;显示获胜信息&#xff0c;并提供退出游戏和重新开始游戏的操作选项。 1.棋盘显示 &#xff1a; 显示一个 15x15 的五子棋…

Redis--补充类型

目录 一、引言 二、补充类型 1.streams 2.geospatial 3.hyperloglog 4.bitmap 5.bitfields 三、总结 一、引言 在简单学习了redis中的5个数据类型&#xff08;string&#xff0c;list&#xff0c;hash&#xff0c;set&#xff0c;zset&#xff09;之后&#xff0c;本篇文…

视频AI方案:数据+算力+算法,人工智能的三大基石

背景分析 随着信息技术的迅猛发展&#xff0c;人工智能&#xff08;AI&#xff09;已经逐渐渗透到我们生活的各个领域&#xff0c;从智能家居到自动驾驶&#xff0c;从医疗诊断到金融风控&#xff0c;AI的应用正在改变着我们的生活方式。而数据、算法和算力&#xff0c;正是构…