Android控件 - TextView、Button、EditText、CompoundButton、CheckBox简介
TextView
TextView類是View的直接子類,用于單純地顯示一行或多行靜態(tài)文本的視圖。它繼承View所有XML屬性,并有著自己的XML屬性。
在XM布局中常用的屬性:
| android:autoLink | 設(shè)置文本超鏈接樣式,并點(diǎn)擊跳向鏈接,值:none 不匹配任何類型的文字(默認(rèn)); web 匹配URL地址,單擊后打開(kāi)瀏覽器顯示地址;email 匹配郵箱地址,單擊后打開(kāi)郵箱發(fā)送郵件;phone 匹配電話號(hào)碼,單擊后打開(kāi)拔號(hào)界面;map 匹配地圖地址,單擊后打開(kāi)地圖選項(xiàng);all 匹配所有的格式,自動(dòng)檢測(cè)web、phone、email和map四種格式 |
| android:autoText | 是否使用自動(dòng)拼寫(xiě)幫助,默認(rèn)false |
| android:cursorVisible | 設(shè)定光標(biāo)為顯示/隱藏,默認(rèn)true |
| android:digits | 設(shè)置允許輸入哪些字符,默認(rèn)false |
| android:drawableBottom | 在控件下方繪制一個(gè)drawable,這里可以是圖片引用、顏色,這里如果是顏色的話會(huì)把文本背景設(shè)為該顏色,并且如果同時(shí)和background使用時(shí)會(huì)覆蓋后者 |
| android:drawableEnd | 在控件結(jié)尾處繪制一個(gè)drawable |
| android:drawableLeft | 在控件左邊處繪制一個(gè)drawable |
| android:drawablePadding | 設(shè)置文本與drawable(圖片)的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可設(shè)置為負(fù)數(shù),單獨(dú)使用沒(méi)有效果 |
| android:drawableRight | 在控件右邊處繪制一個(gè)drawable |
| android:drawableStart | 在控件開(kāi)始處繪制一個(gè)drawable |
| android:drawableTop | 在控件上方繪制一個(gè)drawable |
| android:editable | 設(shè)置是否可編輯,即EditView |
| android:ellipsize | 內(nèi)容過(guò)長(zhǎng)的時(shí),顯示帶省略號(hào),值:none、start、middle、end、marquee(跑馬燈) |
| android:ems | 設(shè)置控件的寬度為N個(gè)字符的寬度。這里測(cè)試為一個(gè)漢字字符寬度 |
| android:fontFamily | 設(shè)定文本的字體 |
| android:freezesText | 設(shè)置屏幕旋轉(zhuǎn)后的文本是否保存文本的內(nèi)容以及光標(biāo)的位置,默認(rèn)false |
| android:gravity | 針對(duì)自身視圖如何定位當(dāng)前內(nèi)容位置,值:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、centent、fill、clip_vertical、clip_horizontal、start、end |
| android:height | 設(shè)置文本區(qū)域的高度 |
| android:hint | 文本為空時(shí)顯示的文字水印提示信息 |
| android:imeOptions | 輸入鍵盤(pán)確定銨鈕類型,在EditText中使用,值:actionNone(回車鍵)、actionGo(Go) 、actionSearch(搜索) 、actionSend(發(fā)送) 、actionNext(下一步) 和 actionDone(確定/完成) |
| android:includeFontPadding | 置文本是否包含頂部和底部額外空白,默認(rèn)為true |
| android:inputMethod | 為文本指定輸入法,需要完全限定名(完整的包名) |
| android:inputType | 設(shè)置文本的類型,用于幫助輸入法顯示合適的鍵盤(pán)類型 |
| android:letterSpacing | 文本字母間距 |
| android:lineSpacingExtra | 設(shè)置行間距 |
| android:lineSpacingMultiplier | 設(shè)置行間距的倍數(shù) |
| android:lines | 設(shè)置文本的行數(shù) |
| android:linksClickable | 設(shè)置鏈接是否點(diǎn)擊連接,即使設(shè)置了autoLink |
| android:marqueeRepeatLimit | 在ellipsize屬性指定marquee(跑馬燈)情況下,字幕動(dòng)畫(huà)重復(fù)滾動(dòng)的次數(shù) |
| android:maxHeight | 設(shè)置文本區(qū)域的最大高度 |
| android:maxLength | 限制顯示的文本長(zhǎng)度,超出部分不顯示 |
| android:maxLines | 設(shè)置文本的最大顯示行數(shù),與width或者layout_width結(jié)合使用,超出部分自動(dòng)換行,超出行數(shù)將不顯示 |
| android:maxWidth | 設(shè)置文本區(qū)域的最大寬度 |
| android:minHeight | 設(shè)置文本區(qū)域的最小高度 |
| android:minLines | 設(shè)置文本的最小行數(shù),與lines類似 |
| android:minWidth | 設(shè)置文本區(qū)域的最小寬度 |
| android:numeric | 輸入限制,值:integer(輸入是數(shù)字的)、signed(輸入是數(shù)字的,符號(hào)是允許的。)、decimal(輸入數(shù)字、小數(shù)允許) |
| android:password | 輸入密碼專用,輸入字符顯示為密碼點(diǎn) |
| android:phoneNumber | 設(shè)置為電話號(hào)碼的輸入方式 |
| android:privateImeOptions | 設(shè)置輸入法選項(xiàng) |
| android:scrollHorizontally | 設(shè)置文本超出TextView的寬度的情況下,是否出現(xiàn)橫拉條 |
| android:selectAllOnFocus | 如果文本是可選擇的,讓他獲取焦點(diǎn)而不是將光標(biāo)移動(dòng)為文本的開(kāi)始位置或者末尾位置 |
| android:shadowColor | 指定文本陰影的顏色,需要與shadowRadius一起使用 |
| android:shadowDx | 設(shè)置陰影橫向坐標(biāo)開(kāi)始位置 |
| android:shadowDy | 設(shè)置陰影縱向坐標(biāo)開(kāi)始位置 |
| android:shadowRadius | 設(shè)置陰影的模糊半徑。設(shè)置為0.1就變成字體的顏色了,一般設(shè)置為3.0的效果比較好 |
| android:singleLine | 是否單行或者多行,回車是離開(kāi)文本框還是文本框增加新行 |
| android:text | 設(shè)置顯示文本 |
| android:textAllCaps | 文本全部大寫(xiě) |
| android:textAppearance | 設(shè)置文字外觀,基于文字的顏色,typeface,大小和樣式 |
| android:textColor | 設(shè)置字體顏色 |
| android:textColorHighlight | 設(shè)置被選中文字的底色,默認(rèn)為藍(lán)色 |
| android:textColorHint | 設(shè)置提示信息文字的顏色,默認(rèn)為灰色 |
| android:textColorLink | 鏈接的文本顏色 |
| android:textIsSelectable | 文本內(nèi)容是否可選擇 |
| android:textScaleX | 控制字與字之間的間距 |
| android:textSize | 設(shè)置文字大小 |
| android:textStyle | 設(shè)置字形,值:normal(常規(guī)) 、bold(粗體)、italic(斜體)。可以設(shè)置一個(gè)或多個(gè),用“|”隔開(kāi) |
| android:typeface | 設(shè)置文本字體,值:normal、sans、serif 、monospace(等寬字體) |
| android:width | 設(shè)置文本區(qū)域的寬度 |
使用實(shí)例1 - 網(wǎng)址超鏈接:
<TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:autoLink="web"android:text="www.csdn.net"/>使用實(shí)例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"/>使用實(shí)例3 - 文字標(biāo)簽更豐富多采:
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>測(cè)試圖片< /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的直接子類,代表一個(gè)按鈕控件。它繼承View和TextView所有XML屬性。
使用實(shí)例1 - XML中指定單擊方法:
<Button android:layout_height="wrap_content" android:text="@string/self_destruct" android:onClick="selfDestruct" />使用實(shí)例2 -代碼中指定單擊方法:
Button button = (Button) findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO... } });EditText
EditText類是TextView的直接子類,代碼一個(gè)可編輯的文本框。它繼承View和TextView所有XML屬性。
使用實(shí)例:
<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="請(qǐng)輸入用戶名" /> <EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:maxLength="10"android:numeric="integer"android:passwrod="true"android:hint="請(qǐng)輸入密碼" />使用實(shí)例2 - 字體設(shè)置:
<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);使用實(shí)例3 - 鍵盤(pán)按鈕監(jiān)聽(tīng):
<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類,在這個(gè)類中封裝了一個(gè) checked屬性,用于判斷按鈕是否被選中狀態(tài),這也是它與Button的不同。它的子類有:CheckBox, RadioButton, Switch,ToggleButton,等。
在XM布局中擴(kuò)展的屬性:
| android:button | 按鈕樣式的擴(kuò)展,可以是資源的引用 |
| android:buttonTint | 按鈕樣式用到的顏色,此值必須是一顏色值 |
| android:buttonTintMode | 按鈕樣式混合模式圖形色彩,值:src_over、src_in、src_atop、multiply、screen、add |
CheckBox
CheckBox類是繼承于CompoundButton類,而CompoundButton類繼承于Button類,代表一個(gè)復(fù)選框按鈕控件。它繼承View、TextView和CompoundButton所有XML屬性。
使用實(shí)例:
<CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/checkboxstyle" android:text="請(qǐng)勾選我"/>// 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>總結(jié)
以上是生活随笔為你收集整理的Android控件 - TextView、Button、EditText、CompoundButton、CheckBox简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASCII码表、ASCII码扩展表
- 下一篇: unity创建草地_Unity3D研究院