UI组件:TextView及其子类

本文详细介绍了Android中的TextView及其子类,包括TextView的功能与XML属性、CheckedTextView的用法、EditText的实例、Button的实现以及单选按钮、复选按钮、状态开关按钮和计时器等组件的使用方法,结合实例展示了各组件的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TextView的功能与用法

  • TextView直接继承了View,它还是EditView,Button两个UI组件的父类。
  • TextView与EditView有很多相似之处,他们之间的最大区别是EditView允许用户编辑编辑文本内容,而TextView不允许。
  • TextView提供了大量的XML属性,这些属性不仅适用于TextView,TextView的子类也同时适用。

TextView的XML属性

下面列举了TextView一些常用的XML属性及相关方法的说明:

XML属性 相关方法  说明
android:autoLink setAutoLinkMask(int) 是否将符合指定格式的文本转化成可点击的链接的形式。
该属性支持如下属性值:
none :没有匹配模式(默认值)
web : 匹配URL网址
email : 匹配邮箱地址
phone : 匹配电话号码
map : 匹配映射地址
all : 匹配所有的模式
android:drawableTop setCompoundDrawablesWithIntrinsicBounds
(Drawable,Drawable,Drawable,Drawable)
在文本框内文本的顶端绘制指定图像
android:drawableEnd setCompoundDrawablesRelativeWithIntrinsicBounds
(Drawable,Drawable,Drawable,Drawable)
在文本框内文本的结尾绘制指定图像
android:drawableLeft setCompoundDrawablesWithIntrinsicBounds
(Drawable,Drawable,Drawable,Drawable)
在文本框内文本的左边绘制指定图像
android:drawableRight setCompoundDrawablesWithIntrinsicBounds
(Drawable,Drawable,Drawable,Drawable)
在文本框内文本的右边绘制指定图像
android:drawablePadding setCompoundDrawablePadding(int) 设置文本框内文本与图像之间的间距
android:ellipsize setEllipsize(TextUtils.TruncateAt) 设置当显示的文本超过TextView的长度时如何处理文本内容。
该属性支持如下属性值:
none :不做任何处理
start : 在文本开始时截断,并显示省略号
middle : 在文本中间截断,并显示省略号
end : 在文本结束时截断,并显示省略号
marquee : 使用marquee滚动动画显示文本
android:fontFamily setTypeface(Typeface) 设置文本框内文本的字体
android:gravity setGravity(int) 设置文本框内文本的对齐方式
android:hint setHint(int) 设置当文本框为空时,把文本框默认显示的文本内容
android:lines setLines(int) 设置该文本框默认占几行
android:shadowColor setShadowLayer(float,float,float,int) 设置文本框阴影的颜色
android:shadowDx setShadowLayer(float,float,float,int) 设置文本框阴影在水平方向的偏移
android:shadowDy setShadowLayer(float,float,float,int) 设置文本框阴影在垂直方向的偏移
android:shadowRadius setShadowLayer(float,float,float,int) 设置文本框阴影的模糊程度。该值越大,阴影越模糊。
android:text setText(CharSequence) 设置文本框的文本内容
android:textColor setTextColor(ColorStateList) 设置文本框文本的颜色
android:inputType setRawInputType(int) 指定该文本框的类型。该属性有点像HTML中的input元素type属性。

CheckedTextView的功能与用法

TextView派生出了一个CheckedTextView,CheckTextView增加了一个checked状态。

通过setChecked(boolean)isChecked()方法来改变、访问该组件的checked状态。
通过setCheckMarkDrawable()方法来设置它的勾选图标。

TextView和 CheckedTextView的基本实例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.textviewinfo.MainActivity">

    <!--设置字体大小为20sp,文本框右边绘制图片,文本内间距为10dp-->
   <TextView
       android:id="@+id/text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:padding="10dp"
       android:textSize="20sp"
       android:drawableRight="@drawable/picture"
       android:text="文本结尾绘制一张图片"/>
    <!--设置文本行数为1,文本结尾省略显示-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="1"
        android:ellipsize="end"
        android:text="长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本"/>
    <!--对邮件、电话添加链接-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:autoLink="email|phone"
        android:text="邮件是[email protected],电话是16839323939"/>
    <!--设置文本颜色,大小,上边距为10dp,并使用阴影-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:shadowColor="#3F51B5"
        android:shadowDx="10.0"
        android:shadowDy="8.0"
        android:shadowRadius="3.0"
        android:text="带有阴影的文字"/>
    <!--测试密码框-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值