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 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 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,也允许放置其他控件。

<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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值