Android---TextView即其子类

1、TextView文本框

TextView直接继承了View,并且是EditText、Button的两个UI组件的父类。
作用:在界面上显示文本
详细参数请参考Android Developer

在这里插入图片描述

    <!--textView 单行,尾部省略,邮箱为超链接-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello world! 1314520@qq.com When a cigarette falls in love with a match,it is destined to be hurt"
        android:singleLine="true"
        android:ellipsize="end"
        android:autoLink="email"
        android:id="@+id/textView1"/>
    <!--使用drawable资源的TextView-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:id="@+id/txt2"
        android:text="背景渐变的文字"
        android:gravity="center"
        android:background="@drawable/bg_border"/>

<?xml version="1.0" encoding="utf-8"?>
<!--文件名为bg_border.xml-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--设置红色边框,宽度2dp-->
    <stroke android:width="2dp" android:color="#f00"/>
    <!--设置渐变背景色,sweep类型渐变,红-绿-蓝-->
    <gradient android:type="sweep"
        android:startColor="#f00"
        android:centerColor="#0f0"
        android:endColor="#00f"/>
    <!--指定角度-->
    <corners android:topLeftRadius="20dp"
        android:topRightRadius="20dp"
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"/>
</shape>

2、EditText输入框

EditText与TextView最大的区别是EditText可以接收用户输入
EditText最重要的属性是inputType,用于将EditText设置为指定类型的输入组件。

<!--输入类型为手机号的输入框-->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit1"
        android:hint="请输入手机号"
        android:inputType="phone"/>

EditText 派生了如下两个子类。

  • AutoCompleteTextView:需要与Adapter结合使用。
  • ExtractEditText:是EditText组件的底层服务类,负责提供全屏输入法支持

3、Button按钮

继承TextView,主要是在UI界面上生成一个按钮,可以提供响应事件(如单击)

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:id="@+id/btn1"/>

4、RadioButton单选框

    <!--若单选按钮中默认选中了某个单选按钮,则必须每个按钮都指定android:id 属性,
    否则此组单选按钮不能正常工作-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rg_txt"
        android:text="性别:"/>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/male"
            android:checked="true"
            android:text="male"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/female"
            android:text="female"/>
    </RadioGroup>
package com.example.textviewui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RadioGroup radioGroup1 = findViewById(R.id.rg);
        TextView show = findViewById(R.id.rg_txt);
        /*为RadioGroup组件的onCheckedChange时间绑定事件监听器*/
        radioGroup1.setOnCheckedChangeListener((radioGroup,checkedId) -> {
            String tip = checkedId == R.id.male ? "您的性别是男人" : "您的性别是女人";
            show.setText(tip);
        });

    }
}

5、CheckBox多选框

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/red"
        android:text="红色"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/green"
        android:text="绿色"/>
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/blue"
        android:text="蓝色"/>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万卷书情似故人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值