Android中级控件

文章目录

图形定制

1.图形Drawable
2.形状图像
  • Shape图形又称为形状图形,它用来描述常见的几何形状,包括矩形、圆角矩形、圆形、椭圆等。
  • 形状图形的定义文件是以shape标签为根节点的XML描述文件,它支持四种类型的形状:rectangle(矩形)、oval(椭圆)、line(直线)、ring(圆环)
3.九宫格图片
4.状态列表图形

状态列表图形不仅用于按钮控件,还可用于拥有多种状态的控件。

状态类型的属性名称 说明 适用的控件
state_pressed 是否按下 按钮Button
state_checked 是否勾选 复选框CheckBox、单选按钮RadioButton
state_focused 是否获取焦点 文本编辑框EditText
state_selected 是否选中 各控件通用

选择按钮

1.复选框CheckBox
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CheckBoxActivity">


    <CheckBox
        android:id="@+id/ck_system"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="系统的CheckBox"/>


    <CheckBox
        android:id="@+id/ck_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:button="@drawable/checkbox_selector"
        android:layout_marginTop="5dp"
        android:checked="true"
        android:text="这个CheckBox换了图标"/>
</LinearLayout>
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/check_choose" android:state_checked="false"/>

    <item android:drawable="@drawable/check_unchoose" android:state_checked="true"/>
</selector>
2.开关按钮Switch

Switch是开关按钮,它在选中与取消选中时可展现的界面元素比复选框丰富。

Switch空间新添加的XML属性说明如下:

  • textOn:设置右侧开启时的文本
  • textOff:设置左侧关闭时的文本
  • track:设置开关轨道的背景
  • thumb:设置开关标识的图标
3.单选按钮

单选按钮要在一组按钮中选择其中的一项,并且不能多选,这要求有个容器确定这组按钮的范围,这个容器便是单选组RadioGroup

RadioGroup实质上是个布局,同一组RadioButton都要放在同一个RadioGroup节点下,除了RadioButton,也允许放置其他控件。

xml 复制代码
<RadioGroup
        android:id="@+id/rg_gender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_male"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="男"/>


        <RadioButton
            android:id="@+id/rb_female"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="女"/>

    </RadioGroup>

判断选中了哪个单选按钮,通常不是监听某个单选按钮,而是监听单选组的选中事件。

  • check:选中资源编号的单选按钮
  • getCheckedRadioButtonId:获取选中状态单选按钮的资源编号、
  • setOnCheckedChangeListener:设置单选按钮勾选变化的监听器

文本输入

1.编辑框EditText

编辑框EditText用于接收软键盘输入的文字,例如用户名、密码、评价等,它由文本视图派生而来,除了TextView已有的各种属性和方法,EditText还支持下列XML属性。

  • inputType:指定输入的文本类型
  • maxLength:指定文本允许输入的最大长度
  • hint:指定提示文本的内容
  • textColorHint:指定提示文本的颜色
2.焦点变更监听器
3.文本变化监听器

调用编辑框对象的addTextChangeListener方法即可注册文本监听器

文本监听器的接口名称为TextWatcher,该接口提供了3个监控方法:

  • beforeTextChanged:在文本改变之前触发
  • onTextChanged:在文本改变过程中触发
  • afterTextChanged:在文本改变之后触发

对话框

1.提醒对话框
  • AlertDialog可以完成常见的交互操作,例如提示、确认、选择等功能。AlertDialog借助建造器AlertDialog.Builder才能完成参数设置。

  • 调用建造器的create方法生成对话框实例,再调用对话框实例的show方法,在页面上弹出对话框。

2.日期对话框DatePickerDialog
3.时间对话框TimePickerDialog
  • 时间选择器TimePicker可以让用户选择具体的小时和分钟
  • TimerPickerDialog的用户类似DatePickerDialog
相关推荐
踏雪羽翼4 小时前
android TextView实现文字字符不同方向显示
android·自定义view·textview方向·文字方向·textview文字显示方向·文字旋转·textview文字旋转
lxysbly4 小时前
安卓玩MRP冒泡游戏:模拟器下载与使用方法
android·游戏
夏沫琅琊6 小时前
Android 各类日志全面解析(含特点、分析方法、实战案例)
android
程序员JerrySUN7 小时前
OP-TEE + YOLOv8:从“加密权重”到“内存中解密并推理”的完整实战记录
android·java·开发语言·redis·yolo·架构
TeleostNaCl8 小时前
Android | 启用 TextView 跑马灯效果的方法
android·经验分享·android runtime
TheNextByte19 小时前
Android USB文件传输无法使用?5种解决方法
android
quanyechacsdn10 小时前
Android Studio创建库文件用jitpack构建后使用implementation方式引用
android·ide·kotlin·android studio·implementation·android 库文件·使用jitpack
程序员陆业聪11 小时前
聊聊2026年Android开发会是什么样
android
编程大师哥11 小时前
Android分层
android
极客小云12 小时前
【深入理解 Android 中的 build.gradle 文件】
android·安卓·安全架构·安全性测试