UI组件TextView及其子类.docx

上传人:b****8 文档编号:8954949 上传时间:2023-05-16 格式:DOCX 页数:21 大小:168.16KB
下载 相关 举报
UI组件TextView及其子类.docx_第1页
第1页 / 共21页
UI组件TextView及其子类.docx_第2页
第2页 / 共21页
UI组件TextView及其子类.docx_第3页
第3页 / 共21页
UI组件TextView及其子类.docx_第4页
第4页 / 共21页
UI组件TextView及其子类.docx_第5页
第5页 / 共21页
UI组件TextView及其子类.docx_第6页
第6页 / 共21页
UI组件TextView及其子类.docx_第7页
第7页 / 共21页
UI组件TextView及其子类.docx_第8页
第8页 / 共21页
UI组件TextView及其子类.docx_第9页
第9页 / 共21页
UI组件TextView及其子类.docx_第10页
第10页 / 共21页
UI组件TextView及其子类.docx_第11页
第11页 / 共21页
UI组件TextView及其子类.docx_第12页
第12页 / 共21页
UI组件TextView及其子类.docx_第13页
第13页 / 共21页
UI组件TextView及其子类.docx_第14页
第14页 / 共21页
UI组件TextView及其子类.docx_第15页
第15页 / 共21页
UI组件TextView及其子类.docx_第16页
第16页 / 共21页
UI组件TextView及其子类.docx_第17页
第17页 / 共21页
UI组件TextView及其子类.docx_第18页
第18页 / 共21页
UI组件TextView及其子类.docx_第19页
第19页 / 共21页
UI组件TextView及其子类.docx_第20页
第20页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

UI组件TextView及其子类.docx

《UI组件TextView及其子类.docx》由会员分享,可在线阅读,更多相关《UI组件TextView及其子类.docx(21页珍藏版)》请在冰点文库上搜索。

UI组件TextView及其子类.docx

UI组件TextView及其子类

UI组件:

TextView及其子类

TextView的功能与用法

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

TextView与EditView有很多相似之处,他们之间的最大区别是EditView允许用户编辑编辑文本内容,而TextView不允许。

TextView提供了大量的XML属性,这些属性不仅适用于TextView,TextView的子类也同时适用。

TextView的XML属性

下面列举了TextView一些常用的XML属性及相关方法的说明:

CheckedTextView的功能与用法

TextView派生出了一个CheckedTextView,CheckTextView增加了一个checked状态。

通过setChecked(boolean)和isChecked()方法来改变、访问该组件的checked状态。

通过setCheckMarkDrawable()方法来设置它的勾选图标。

TextView和CheckedTextView的基本实例

xmlversion="1.0"encoding="utf-8"?

>

android="

xmlns:

tools="

android:

id="@+id/activity_main"

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

orientation="vertical"

tools:

context="com.example.textviewinfo.MainActivity">

--设置字体大小为20sp,文本框右边绘制图片,文本内间距为10dp-->

android:

id="@+id/text"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

padding="10dp"

android:

textSize="20sp"

android:

drawableRight="@drawable/picture"

android:

text="文本结尾绘制一张图片"/>

--设置文本行数为1,文本结尾省略显示-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

lines="1"

android:

ellipsize="end"

android:

text="长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本"/>

--对邮件、电话添加链接-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_marginTop="10dp"

android:

autoLink="email|phone"

android:

text="邮件是kongyeeku@,电话是16839323939"/>

--设置文本颜色,大小,上边距为10dp,并使用阴影-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_marginTop="10dp"

android:

shadowColor="#3F51B5"

android:

shadowDx="10.0"

android:

shadowDy="8.0"

android:

shadowRadius="3.0"

android:

text="带有阴影的文字"/>

--测试密码框-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_marginTop="10dp"

android:

inputType="textPassword"

android:

text="hello"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

textSize="20sp"

android:

checkMark="@drawable/ok"

android:

text="可勾选的文本"/>

EditText的功能与用法

EditView继承自TextView,可以使用TextView的相关XML属性。

EditText组件最重要的属性是inputType,用来为EditText指定类型的输入组件。

EditText的实例

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"android:

layout_width="match_parent"

android:

layout_height="match_parent">

--当文本可选择时,聚焦时所有的文本被选择-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

hint="聚焦时所有的文本被选择"

android:

selectAllOnFocus="true"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

inputType="number"

android:

hint="数字输入框"/>

--只能输入数字的密码框-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

hint="数字密码输入框"

android:

inputType="numberPassword"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

hint="密码输入框"

android:

inputType="textPassword"/>

--能输入年月日-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

hint="日期输入框"

android:

inputType="date"/>

--能输入年月日和小时分秒-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

hint="时间日期输入框"

android:

inputType="datetime"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

hint="电话号码输入框"

android:

inputType="phone"/>

Android:

hint指定文本的提示信息。

当焦点定位到相应的输入框,系统切换到相应的输入法。

如点击数字密码输入框,系统会切换到数字输入法。

Button的功能与用法

Button继承自TextView,它主要是在UI界面上生成一个按钮,可添加点击事件。

可为Button添加android:

background属性为按钮添加背景颜色或图片。

可使用自定义的Drawable对象,可以使按钮背景颜色或者图片随用户动作动态改变。

Button的实例

下面设置了普通按钮和使用了Drawable对象的按钮。

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"android:

layout_width="match_parent"

android:

layout_height="match_parent">

--普通文字按钮-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

background="@color/colorGreen"

android:

textColor="#FFF0F5F5"

android:

text="普通文字按钮"/>

--使用Drawable对象的按钮-->

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_marginTop="5dp"

android:

textColor="#FFF0F5F5"

android:

background="@drawable/button_selector"

android:

text="使用Drawable对象的按钮"/>

第二个按钮指定android:

background属性为@drawable/button_selector,该属性值应用了一个Drawable资源,该资源对应的XML文件如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

--指定按钮按下的颜色-->

state_pressed="true"

android:

drawable="@color/colorRed"/>

--指定按钮松开的颜色-->

state_pressed="false"

android:

drawable="@color/colorGreen"/>

第一个按钮是固定的。

第二个按钮点击前,按钮为绿色,按下时按钮为红色,松开后按钮又变绿色。

上面的资源文件使用selecto元素定义了一个StateListDrawable对象。

单选按钮(RadioButton)和复选按钮(CheckBox)的功能与用法

单选按钮(RadioButton)和复选按钮(CheckBox)、状态开关按钮(ToggleButton)和开关(Switch)都继承自Button。

单选按钮(RadioButton)和复选按钮(CheckBox)与普通按钮的区别在于它们多了一个可供选择的功能。

RadioButton和CheckBox的区别在于前者为单选按钮,后者为多选按钮。

RadioButton通常要与RadioGroup一起使用,用于定义一组单选按钮。

利用单选按钮、复选框获取用户信息的实例

布局文件如下所示:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

orientation="vertical">

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="horizontal">

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

textSize="16sp"

android:

padding="8dp"

android:

text="性别:

"/>

--定义一组单选按钮-->

android:

id="@+id/sex"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

orientation="horizontal"

android:

layout_gravity="center_horizontal">

android:

id="@+id/male"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

textSize="16sp"

android:

checked="true"

android:

text="男"/>

android:

id="@+id/female"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

textSize="16sp"

android:

layout_marginLeft="20dp"

android:

text="女"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

text="你喜欢的颜色是:

"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="vertical">

android:

id="@+id/red"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

checked="true"

android:

text="红色"/>

android:

id="@+id/green"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

text="绿色"/>

android:

id="@+id/yellow"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

text="黄色"/>

android:

id="@+id/blue"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

text="蓝色"/>

android:

id="@+id/show"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

textSize="16sp"

android:

padding="10dp"/>

上面的界面定义了一组单选按钮,并默认勾选了第一个单选按钮,可供用户选择性别。

接下来定义了四个复选框,默认勾选了第一个复选框。

接着在Activity中为单选按钮添加事件监听。

packagecom.example.textviewinfo;

importandroid.os.Bundle;

importandroid.support.v7.app.AppCompatActivity;

importandroid.widget.RadioGroup;

importandroid.widget.TextView;

publicclassMainActivityextendsAppCompatActivity{

privateRadioGroupsex;

privateTextViewshow;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.radio_checkbox);

sex=(RadioGroup)findViewById(R.id.sex);

show=(TextView)findViewById(R.id.show);

//为RadioGroup组件绑定OnCheck事件绑定事件监听

sex.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){

@Override

publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){

//根据用户勾选的单选按钮来改变sexStr的值

StringsexStr=checkedId==R.id.male?

"您的性别是男":

"您的性别是女";

//修改show组件中的文本

show.setText(sexStr);

}

});

}

}

上面代码添加事件监听器的方式采用了“委托式”事件处理机制,委托式事件处理机制的原理是:

当事件源上发生事件时,该事件将会激发该事件源上的监听事件器的特定方法。

状态开关按钮(ToggleButton)和开关(Switch)的功能与用法

ToggleButton,Switch和CheckBox非常相似,只是ToggleButton和Switch通常用于切换程序当中的某种状态。

ToggleButton支持的XML属性及相关得方法如下表所示:

动态控制布局的实例

界面布局文件代码如下所示:

xmlversion="1.0"encoding="utf-8"?

>

android="

xmlns:

tools="http:

//

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

paddingBottom="@dimen/activity_vertical_margin"

android:

paddingLeft="@dimen/activity_horizontal_margin"

android:

paddingRight="@dimen/activity_horizontal_margin"

android:

paddingTop="@dimen/activity_vertical_margin"

android:

orientation="vertical"

tools:

context="com.example.textviewinfo.StateButtonActivity">

android:

id="@+id/toggle"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

textOff="横向排列"

android:

textOn="纵向排列"

android:

checked="true"/>

android:

id="@+id/switcher"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

textOff="横向排列"

android:

textOn="纵向排列"

android:

checked="true"/>

android:

id="@+id/test"

android:

orientation="vertical"

android:

layout_width="wrap_content"

android:

layout_height="wrap_conten

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2