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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 每天一道算法_3_487-3279_对电话号码格式化统计批处理

    早上弄了一道求高精度幂的算法,偷懒用了内部类,总觉得过意不去,所以今天重新做了一道算法题,做完心里舒服好多。题目如下: Description企业喜欢用容易被记住的电话号码。让电话号码容易被记住的一个办法是将它写成一个容易记住的单词或者短语。例如,你需要给滑铁卢大学打电话时,可以拨打TUT-GLOP。有时,只将电话号码中部分数字拼写成单词。当你晚上回到酒店,可以通过拨打310-GI

    2022年3月10日
    357
  • pycharm企业版激活码[最新免费获取]

    (pycharm企业版激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~S…

    2022年3月25日
    99
  • 技术实现总结

    技术实现总结

    2021年9月11日
    56
  • 水牛城66有看点不_acwing是什么

    水牛城66有看点不_acwing是什么给定一张 L 个点、P 条边的有向图,每个点都有一个权值 f[i],每条边都有一个权值 t[i]。求图中的一个环,使“环上各点的权值之和”除以“环上各边的权值之和”最大。输出这个最大值。注意:数据保证至少存在一个环。输入格式第一行包含两个整数 L 和 P。接下来 L 行每行一个整数,表示 f[i]。再接下来 P 行,每行三个整数 a,b,t[i],表示点 a 和 b 之间存在一条边,边的权值为 t[i]。输出格式输出一个数表示结果,保留两位小数。数据范围2≤L≤1000,2≤P≤50

    2022年8月9日
    4
  • 《JavaScript 模式》读书笔记(3)— 字面量和构造函数3

    这是字面量和构造函数的最后一篇内容,其中包括了JSON、正则表达式字面量,基本值类型包装器等知识点。也是十分重要的哦。五、JSONJSON是指JavaScript对象表示以及数据传输格式。它是一种

    2022年3月25日
    32
  • 谷歌域名大全_www.google.vcom

    谷歌域名大全_www.google.vcom谷歌域名大全google各国网址  美国www.google.com  加拿大www.google.ca  墨西哥www.google.com.mx  巴西www.google.com.br  智利www.google.cl  阿根廷www.google.com.ar  秘鲁www.google.com.pe  哥伦比亚www.google.com.c

    2022年10月3日
    0

发表回复

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

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