6.1Android常用控件及属性用法


本篇文章和 下一篇文章结合讲解,本章多为概念, 下一篇文章多为实际代码


  控件是界面组成的主要元素,开始都是创建容器(ViewGroup的实例),然后不断的向容器中添加界面组件,例如TextViet(文本框)、EditText(编辑框)和Button(按钮)等,这些控件与用户直接交互。

TextView(文本框)

  TextView是Android中常用组件,用来显示手机上的文本信息(字符串),TextView直接继承了View,它也是EditText、Button两个UI组件的父类。TextView作用就是在界面上显示文本——有点类似Swing编程中的JLabel,不过它比JLabel更强大
  TextView其实就是一个文本编辑框,只是Android关闭了它的文字编辑功能。如果想要定义一个可以编辑内容的文本框,则可以使用子类:EditText。TextView和EditText具有很多相似之处,它们最大的区别在于TextView不允许用户编辑文本内容,EditText允许用户编辑文本内容。
TextView常用属性:

属性介绍
android:text设置显示文本
android:textSize设置字体大小,推荐单位sp,例android:textSize=“20sp”
android:textColor设置字体颜色
android:textStyle设置字体样式 ,粗体(bold),斜体(italic),粗斜体(bolditalic
android:height设置文本区域高度,支持单位:px/dp(推荐)/sp/in/mm
android:width设置文本区域宽度,支持单位:px/dp(推荐)/sp/in/mm
android:password设置文本以密码形式"."显示
android:gravity设置文本位置,例android:gravity=“center”,文本居中显示
android:maxLenght设置文本长度,超出不显示,如android:maxLength=“10”
android:maxLines设置该文本框最多占几行
android:minLines设置该文本框最少占几行
android:phonrNumber设置以电话号码方式输入,truefalse
android:layout_height设置TextView控件高度
android:layout_width设置TextView控件宽度
android:capitalize控制是否将用户输入的文本转换成大写字母,属性:
  none:不支持
  sentences:每个句子首字母大写
  words:每个单词首字母大写
  characters:每个字母都大写
android:editable设置文本是否允许编辑
android:fontFamily设置文本框内文本的字体
android:hint设置当文本框内容空白时,文本框默认显示的提示文本
android:scrollHorizontally设置文本框不够显示全部内容时是否允许水平滚动
android:shadowColor设置文本框内文本的阴影颜色
android:shadowRadius设置文本框内文本的阴影的模糊程度。数值越大,阴影越模糊

  :①layout_widthlayout_height属性可以单独使用,而widthheight属性不能,如果单独使用widthheight属性,此时的控件是不显示的
  ②layout_widthlayout_height可以设置为wrap_contentmatch_parent,而widthheight只能设置固定值,否则产生编译错误
  ③若果要使用widthheight,就必须同时设置layout_widthlayout_height属性,把widthheight属性作为组件的微调使用

  1. 字体水平滚动(走马灯)
    android:singleLine=“true”:单行显示;实现跑马灯让它单行滚动,必须设置单行显示
    android:ellipsize=“marquee”:设置走马灯效果
    android:focusable=“true”:设置是否调取焦点,跑马灯设置为true
    android:focusableInTouchMode=“true”:设置在触碰时是否获取焦点 ,跑马灯设置为true
    android:marqueeRepeatLimit=“marquee_forever”:在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中国 中国 中国 中国 中国 中国 中国 中国"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" />

  截图看不出效果,不放截图

EditText(编辑框)

  EditText与TextView非常相似,与TextView共用了许多属性和方法,EditText用来接收用户向程序中输入的信息,将输入的信息传递给Android程序
  EditText继承自TextView,与TextView最大的不同是用户可以在设备上对EditText控件进行操作,同时还可以为EditText控件设置监听器,用来测试用户输入的内容是否合法。

属性方法
android:hint设置EditText没有输入内容时显示的提示文本
android:textSize设置字体大小
android:maxLines设置最大行数
android:minLines设置最小行数
android:capitalize设置首字母大写
android:edittable设置是否可编辑
android:inputType设置用户输入指定类型
android:letterSpacing设置字间距
android:password设置文本以密码形式“.”显示
android:paddingLeft设置内边距;设置字体距左距离
android:paddingRight设置内边距;设置字体距右距离
android:phoneNumber设置文本以电话号码方式输入
android:backgroundbackground=“@null”:设置EditText显示框默认没有下划线
android:scrollHorizontally设置文本框不够显示全部内容时是否允许水平滚动
android:drawableLeft设置文本框左侧图标资源
android:drawableRight设置文本框右侧图标资源
android:drawablePadding设置图片资源的内边距
android:lineSpacingExtra设置行间
android:numeric设置编辑框只能接收的样式;integer整数;decimal小数;signed符号
android:maxLenght设置文本长度,超出不显示,如android:maxLength=“10”
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="输入密码"
        android:background="@null"
        android:password="true"
        android:textSize="20sp"
        android:layout_gravity="center_horizontal"
        android:letterSpacing="1"
        android:numeric="integer"
        android:maxLength="4"/>

运行结果(可以在运行出来的界面中输入):
在这里插入图片描述

Button(按钮)

  Button(按钮)继承了TextView,所以很多属性也适用于Button。Button主要用于响应用户的一系列点击事件,使程序更加流畅和完整

属性方法
android:text设置按钮上的显示文本
android:textSize设置文本大小

  Button按钮常用的监听事件有3种
    ①指定onClick属性(适合单一按钮)
    ②使用匿名内部类(方便,快捷)
    ③在当前Activity实现OnClickListener接口(按钮较多适用)

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:textSize="30sp"
        android:layout_gravity="center_horizontal"
        android:background="#ff9897" />

运行:
在这里插入图片描述

RadioButton(单选按钮)

  RadioButton是单选按钮,需要与RadioGrou配合使用,RadioGroup是单选组合集,可容纳多个RadioButton,并把它们组合在一起,实现单选状态。RadioButton默认垂直排序,可以通过orientation属性设置排序方向。
  单选按钮通过setOnCheckedChangeListener()方法实现监听事件

属性方法
android:orientation控制RadioButton排列方向
android:checked设置默认选中状态;true默认选中;false默认不选中

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="20sp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="20sp" />
    </RadioGroup>

运行:
在这里插入图片描述

如果默认选中“男”,在“男”中添加android:checked="true"属性:

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="20sp" 
            android:checked="true"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="20sp" />
    </RadioGroup>

运行:
在这里插入图片描述

CheckBox(多选按钮)

  CheckBox是多选按钮或复选按钮,可以多选,直接可以单独定义。排序顺序由父布局定义
  多选按钮通过CompoundButton.onCheckedChangeListener定义接口实现监听事件

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="面条"
        android:textSize="20sp" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="米饭"
        android:textSize="20sp" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="凉皮"
        android:textSize="20sp" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="泡馍"
        android:textSize="20sp" />

运行:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值