Android控件 - TextView、Button、EditText、CompoundButton、CheckBox简介

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值