Android控件 – TextView、Button、EditText、CompoundButton、CheckBox简介「建议收藏」

Android控件 – TextView、Button、EditText、CompoundButton、CheckBox简介「建议收藏」TextViewTextView类是View的直接子类,用于单纯地显示一行或多行静态文本的视图。它继承View所有XML属性,并有着自己的XML属性。在XM布局中常用的属性:属性说明android:autoLink设置文本超链接样式,并点击跳向链接,值:none不匹配任何类型的文字(默认);web匹配URL地址,单击后打开浏览器显示地址;email匹配邮箱地址,单击后打

大家好,又见面了,我是你们的朋友全栈君。

TextView

TextView类是View的直接子类,用于单纯地显示一行或多行静态文本的视图。它继承View所有XML属性,并有着自己的XML属性。

在XM布局中常用的属性:

属性 说明
android:autoLink 设置文本超链接样式,并点击跳向链接,值:none 不匹配任何类型的文字(默认); web 匹配URL地址,单击后打开浏览器显示地址;email 匹配邮箱地址,单击后打开邮箱发送邮件;phone 匹配电话号码,单击后打开拔号界面;map 匹配地图地址,单击后打开地图选项;all 匹配所有的格式,自动检测web、phone、email和map四种格式
android:autoText 是否使用自动拼写帮助,默认false
android:cursorVisible 设定光标为显示/隐藏,默认true
android:digits 设置允许输入哪些字符,默认false
android:drawableBottom 在控件下方绘制一个drawable,这里可以是图片引用、颜色,这里如果是颜色的话会把文本背景设为该颜色,并且如果同时和background使用时会覆盖后者
android:drawableEnd 在控件结尾处绘制一个drawable
android:drawableLeft 在控件左边处绘制一个drawable
android:drawablePadding 设置文本与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果
android:drawableRight 在控件右边处绘制一个drawable
android:drawableStart 在控件开始处绘制一个drawable
android:drawableTop 在控件上方绘制一个drawable
android:editable 设置是否可编辑,即EditView
android:ellipsize 内容过长的时,显示带省略号,值:none、start、middle、end、marquee(跑马灯)
android:ems 设置控件的宽度为N个字符的宽度。这里测试为一个汉字字符宽度
android:fontFamily 设定文本的字体
android:freezesText 设置屏幕旋转后的文本是否保存文本的内容以及光标的位置,默认false
android:gravity 针对自身视图如何定位当前内容位置,值:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、centent、fill、clip_vertical、clip_horizontal、start、end
android:height 设置文本区域的高度
android:hint 文本为空时显示的文字水印提示信息
android:imeOptions 输入键盘确定铵钮类型,在EditText中使用,值:actionNone(回车键)、actionGo(Go) 、actionSearch(搜索) 、actionSend(发送) 、actionNext(下一步) 和 actionDone(确定/完成)
android:includeFontPadding 置文本是否包含顶部和底部额外空白,默认为true
android:inputMethod 为文本指定输入法,需要完全限定名(完整的包名)
android:inputType 设置文本的类型,用于帮助输入法显示合适的键盘类型
android:letterSpacing 文本字母间距
android:lineSpacingExtra 设置行间距
android:lineSpacingMultiplier 设置行间距的倍数
android:lines 设置文本的行数
android:linksClickable 设置链接是否点击连接,即使设置了autoLink
android:marqueeRepeatLimit 在ellipsize属性指定marquee(跑马灯)情况下,字幕动画重复滚动的次数
android:maxHeight 设置文本区域的最大高度
android:maxLength 限制显示的文本长度,超出部分不显示
android:maxLines 设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示
android:maxWidth 设置文本区域的最大宽度
android:minHeight 设置文本区域的最小高度
android:minLines 设置文本的最小行数,与lines类似
android:minWidth 设置文本区域的最小宽度
android:numeric 输入限制,值:integer(输入是数字的)、signed(输入是数字的,符号是允许的。)、decimal(输入数字、小数允许)
android:password 输入密码专用,输入字符显示为密码点
android:phoneNumber 设置为电话号码的输入方式
android:privateImeOptions 设置输入法选项
android:scrollHorizontally 设置文本超出TextView的宽度的情况下,是否出现横拉条
android:selectAllOnFocus 如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置
android:shadowColor 指定文本阴影的颜色,需要与shadowRadius一起使用
android:shadowDx 设置阴影横向坐标开始位置
android:shadowDy 设置阴影纵向坐标开始位置
android:shadowRadius 设置阴影的模糊半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好
android:singleLine 是否单行或者多行,回车是离开文本框还是文本框增加新行
android:text 设置显示文本
android:textAllCaps 文本全部大写
android:textAppearance 设置文字外观,基于文字的颜色,typeface,大小和样式
android:textColor 设置字体颜色
android:textColorHighlight 设置被选中文字的底色,默认为蓝色
android:textColorHint 设置提示信息文字的颜色,默认为灰色
android:textColorLink 链接的文本颜色
android:textIsSelectable 文本内容是否可选择
android:textScaleX 控制字与字之间的间距
android:textSize 设置文字大小
android:textStyle 设置字形,值:normal(常规) 、bold(粗体)、italic(斜体)。可以设置一个或多个,用“|”隔开
android:typeface 设置文本字体,值:normal、sans、serif 、monospace(等宽字体)
android:width 设置文本区域的宽度

使用实例1 – 网址超链接:

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="www.csdn.net"/>

使用实例2 – 显示图片和背景:

<TextView
    android:id="@+id/txt_back"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:paddingLeft="20dp"
    android:paddingRight="15dp"
    android:background="@drawable/return_btn_bg"
    android:drawableLeft="@drawable/wallpaper_preview_back_selector"/>

使用实例3 – 文字标签更丰富多采:

TextView tvUserName = (TextView)findViewById(R.id.tv_user_name);
String str = "<font color=\"#FF0000\">你好</font><br><font color=\"#00FF00\">安卓</font>"
tvUserName .setText(Html.formHtml(str));

// 带图片-------------------------------------------------------------------
TextView tvUserName = (TextView)findViewById(R.id.tv_user_name);
String str = "<h1>测试图片< /h1>< p><img src=" + R.drawable.ic_launcher + "></p>";
tvUserName .setText(Html.formHtml(str, imgGetter, null));

ImageGetter imageGetter = new Html.ImageGetter() {
    public Drawable getDrawable(String source) {
        int id = Integer.parseInt(source);
        Drawable drawable = getResources().getDrawable(id);
        return drawable;
    }
}

Button

Button类是TextView的直接子类,代表一个按钮控件。它继承View和TextView所有XML属性。

使用实例1 – XML中指定单击方法:

<Button     
    android:layout_height="wrap_content"     
    android:text="@string/self_destruct"     
    android:onClick="selfDestruct" />

使用实例2 -代码中指定单击方法:

Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {             
    public void onClick(View v) {    
        // TODO...             
    }         
});

EditText

EditText类是TextView的直接子类,代码一个可编辑的文本框。它继承View和TextView所有XML属性。

使用实例:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入用户名" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLength="10"
    android:numeric="integer"
    android:passwrod="true"
    android:hint="请输入密码" />

使用实例2 – 字体设置:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:typeface="sans" 
    android:hint="我的是字体是sans"/>
<EditText
    android:et_user_name
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="我的是字体是华文行楷" />
// -------------------------------------------------------------------
EditText etUserName = (EditText)findViewById(R.id.et_user_name);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/ STXINGKA.TTF");
etUserName.setTypeface(typeFace);

使用实例3 – 键盘按钮监听:

<EditText
        android:id="@+id/et_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:imeOptions="actionDone"/>
// -------------------------------------------------------------------
EditText etUserName = (EditText)findViewById(R.id.et_user_name);
etUserName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId == EditorInfo.IME_ACTION_DONE) {
            return true;
        }
        return false;
    }
});

CompoundButton

CompoundButton继承自Button类,在这个类中封装了一个 checked属性,用于判断按钮是否被选中状态,这也是它与Button的不同。它的子类有:CheckBox, RadioButton, Switch,ToggleButton,等。

在XM布局中扩展的属性:

属性 说明
android:button 按钮样式的扩展,可以是资源的引用
android:buttonTint 按钮样式用到的颜色,此值必须是一颜色值
android:buttonTintMode 按钮样式混合模式图形色彩,值:src_over、src_in、src_atop、multiply、screen、add

CheckBox

CheckBox类是继承于CompoundButton类,而CompoundButton类继承于Button类,代表一个复选框按钮控件。它继承View、TextView和CompoundButton所有XML属性。

使用实例:

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/checkboxstyle" 
    android:text="请勾选我"/>

// checkboxstyle.xml--------------------------------------------------------
<?xml version = "1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state check="true" android:drawable="@drawable/select" />
    <item android:state check="fale" android:drawable="@drawable/unselect" />
</selector>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161176.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月18日 下午3:00
下一篇 2022年7月18日 下午3:16


相关推荐

  • Springboot启动报错[ main] o.s.boot.SpringApplication: Application run failed(佷有可能是版本问题)

    Springboot启动报错[ main] o.s.boot.SpringApplication: Application run failed(佷有可能是版本问题)前言:本人小白一枚,最近在自学JAVA时遇到了一个小问题,在网上求解无果后,自己找到了原因,这里跟大家分享一下。开发环境:Win10;IntelliJIDEA2021.3.2版本信息:Java\jdk-17.0.2;apache-maven-3.8.4-bin;springboot2.3.4编程目的:本人之前对JAVA一窍不通,最近在自学JAVA时想要用JAVA,Springboot和maven搭建一个最基础的helloworld程序。报错信息:ERROR后面显示“o.s.boot.

    2025年10月9日
    5
  • 最小生成树详解(模板 + 例题)

    最小生成树详解(模板 + 例题)作为一个伪 ACMer 先来首广为人知的打油诗 模拟只会猜题意 贪心只能过样例 数学上来先打表 规律一般是 DP 组合数学碰运气 计算几何瞎暴力 图论一顿套模板 数论只会 GCD 递归递推伤不起 搜索茫然 TLE 分治做得像枚举 暴力枚举数第一 数据结构干瞪眼 怒刷水题找信心 文章目录 1 什么是树 2 最小生成树 3 最小生成树的应用 4 实现最小生成树的两种算法 4 1prim 普里姆算法 4 2kruskal 克鲁斯卡尔算法 5 总结 1 什么是树如果一个无向连通图不包含回路 连通图中不存在环

    2026年3月19日
    3
  • 数据结构哈希表怎么画(数据结构哈希算法)

    数据结构哈希表参考代码如下:/* 名称:哈希表 语言:数据结构C语言版 编译环境:VC++6.0 日期:2014-3-26*/#include#include#include#defineNULLKEY0 //0为无记录标志#defineN10 //数据元素个数typedefintKeyType;//设关键字域为整型

    2022年4月11日
    397
  • anaconda和pycharm配置pytorch的方法

    anaconda和pycharm配置pytorch的方法第一步 搭建环境 创建 pythorch 虚拟环境 Win R 启动 cmd 在命令提示符内输入以下命令 condacreate npytorchpyth 3 6activatepyt 这里一共存在两条指令 前面一条指令用于创建一个名为 pytorch 的环境 该环境的 python 版本为 3 6 后面一条指令用于激活一个名为 pytorch 的环境 接着进行 pytorch 库的安装由于我们所有的操作都要在对应环境中进行 所以在进行库的安装前需要先激活环境 activatepyto

    2026年3月18日
    2
  • 掌握未来:为什么每个程序员都应该学习大模型技术?

    掌握未来:为什么每个程序员都应该学习大模型技术?

    2026年3月14日
    2
  • WinRAR去广告:只需六步,教你去除WinRAR的广告[通俗易懂]

    WinRAR去广告:只需六步,教你去除WinRAR的广告[通俗易懂]最简单的WinRAR去广告步骤1.下载ResourceHacker地址:ResourceHacker下载地址2.用软件打开WinRAR.exe3.打开StringTable4.打开80:20525.删除1277所在整行,点击运行,然后另存为6.将另存为的WinRAR替换掉原来的将新生成的exe文件仍然命名为WinRAR.exe(我把原来的winrar改名为WinRAR_backup.exe作为备份了)7.大功告成,广告去除了…

    2022年4月27日
    50

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号