界面组件——文本框(TextView)和编辑框(EditText)

介绍

TextView直接继承了View,它还是EditTextButton两个UI组件类的父类。

TextViewEditView均能在界面上显示文本,只是后者还能提供对此显示文本的编辑功能


用例1:基本属性设置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--设置字体为20pt-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="一代人"
        android:textSize="20pt"/>

    <!--设置中间省略-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="黑夜给了我黑色的眼睛,我却用它寻找光明。"
        android:ellipsize="middle"/>

    <!--对邮件增加链接-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="个人邮箱yuanguangyu1221@163.com,欢迎来信。"
        android:autoLink="email"/>

    <!--设置文字颜色、大小,并使用阴影-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="诗歌爱好者"
        android:shadowColor="#0000ff"
        android:shadowDx="15.0"
        android:shadowDy="20.0"
        android:textColor="#ff0000"
        android:textSize="25pt"/>

    <!--测试密码框-->
    <TextView
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:password="true"/>

</LinearLayout>

相应地界面展示结果:
图1


用例2给文本框添加边框或图片

使用shape在drawable目录下创建一个背景文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff" />
    <stroke android:width="1dp" android:color="#ff0000" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--通过android:backgroup指定背景-->
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_border"
        android:text="带边框的文本"
        android:textSize="25dp"/>

    <!--通过android:drawableLeft绘制一张图片-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="带图片的文本"
        android:drawableLeft="@drawable/leaf"/>

</LinearLayout>

相应地界面展示结果:

图2


用例3添加默认提示和焦点切换功能,对于电话号码框时输入法自动切换到数字键盘

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="用户名: "
            android:textSize="10pt"
            android:background="@drawable/bg_border"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请填写登录账号"
            android:selectAllOnFocus="true"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="密码: "
            android:textSize="10pt"
            android:background="@drawable/bg_border"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:password="true"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="电话号码"
            android:background="@drawable/bg_border"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请填写您的电话号码"
            android:selectAllOnFocus="true"
            android:phoneNumber="true"/>
    </TableRow>

    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="注册"/>
    </TableRow>
</TableLayout>

相应地界面展示结果:

图2


摘自《疯狂Android讲义》

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值