TextView
TextView类是View的直接子类,用于单纯地显示一行或多行静态文本的视图。它继承View所有XML属性,并有着自己的XML属性。
在XM布局中常用的属性:
使用实例1 - 网址超链接:
<TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:autoLink="web"android:text=""/>
使用实例2 - 显示图片和背景:
<TextViewandroid: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中指定单击方法:
<Buttonandroid: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属性。
使用实例:
<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入用户名" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:maxLength="10"android:numeric="integer"android:passwrod="true"android:hint="请输入密码" />
使用实例2 - 字体设置:
<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:typeface="sans" android:hint="我的是字体是sans"/><EditTextandroid:et_user_nameandroid: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 - 键盘按钮监听:
<EditTextandroid: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() {@Overridepublic 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布局中扩展的属性:
CheckBox
CheckBox类是继承于CompoundButton类,而CompoundButton类继承于Button类,代表一个复选框按钮控件。它继承View、TextView和CompoundButton所有XML属性。
使用实例:
<CheckBoxandroid: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="/apk/res/android"><item android:state check="true" android:drawable="@drawable/select" /><item android:state check="fale" android:drawable="@drawable/unselect" /></selector>