2014/04/04 決定重新系統地學習一遍Android上的各種控件
從TextView開始:
一、TextView概述
android.widget.TextView直接繼承於android.view.View類.
直接子類有:Button、CheckedTextView、Chronometer、DigitalClock、EditText.
構造函數:(1)TextView(Context context)
(2)TextView(Context context,AttributeSet attrs)
(3)TextView(Context context,AttributeSet attrs,int defStyle)
TextView用於向用戶顯示文本,它的直接子類EditText可用於編輯文本。
二、XML屬性
屬性 |
具體描述 |
android:autoLink |
控制如URL及Email地址能否被自動發現及轉換爲可點擊的鏈接 |
android:autoText |
控制一些簡單的拼寫錯誤能否被識別 |
android:cursorVisible |
設置編輯光標是否可見 |
android:enabled |
指定該TextView是否可用 |
android:gravity |
指定當該TextView中的文字小於TextView可容納的範圍時,文字顯示在何處 |
android:height |
通過像素來指定該TextView的高度 |
android:lines |
通過行數來指定該TextView的高度 |
android:linksClickable |
若設爲false,及時autoLink找到了鏈接地址,也不能點擊 |
android:maxHeight |
通過像素指定控件的最大高度 |
android:maxLength |
通過字數來指定文字最大長度 |
android:maxLines |
通過行數來指定控件最大高度 |
android:maxWidth |
通過像素來指定控件最大寬度 |
android:minHeight |
與android:maxHeight相對 |
android:minLines |
與android:maxLines相對 |
android:minWidth |
與android:maxWidth相對 |
android:password |
指定文字是以密碼圓點顯示或者直接顯示文字內容 |
android:scrollHorizontally |
指定當文字長度超過顯示寬度時,能否水平滑動 |
android:selectAllOnFocus |
如果文字可選定,直接選定所有文字而不是通過移動光標來選定 |
android:shadowColor |
在文字底部設置陰影顏色 |
android:singleLine |
將控件行數約束至一行,當用戶按下Enter時,獲取控件焦點而不是插入新的一行 |
android:text |
TextView顯示的文字 |
android:textAllCaps |
將文字全部以大寫形式顯示 |
android:Appearance |
指定文字顏色、字體、大小及字形 |
android:textColor |
指定文字顏色 |
android:textColorHighlight |
指定文字高亮顏色 |
android:textColorHint |
指定文字提示顏色 |
android:textColorLink |
指定鏈接文字顏色 |
android:textCursorDrawable |
指定出現在光標下的圖片 |
android:textIsSelectable |
表明不可編輯的文字可以被選中 |
android:textScaleX |
設置文字的水平縮放比例 |
android:textSize |
設置文字的大小 |
三、常用Public方法
返回值 |
方法名及參數 |
具體描述 |
void |
addTextChangedListener(TextWatcher watcher) |
爲該控件添加一個監聽器,監聽事件爲文字發生改變 |
final void |
append(CharSequence text) |
將指定的文字添加到TextView的顯示緩衝區內,並將BufferType設置爲EDITABLE |
void |
cancelLongPress() |
取消正在進行的長按動作 |
void |
clearComposingTest() |
清空正在輸入的文字 |
void |
debug(int depth) |
將該控件的相關信息打印在日誌輸出記錄上,標籤爲VIEW_LOG_TAG |
boolean |
didTouchFocusSelect() |
只有當正在進行一個觸摸手勢時返回true |
Editable |
getEditableText() |
將文本作爲Editable對象返回,若文本不可編輯,返回null |
inputFilter[] |
getFliters() |
返回一個inputFliter數組 |
CharSequence |
getText() |
返回TextView正在顯示的文字 |
int |
length() |
返回控件中文字的長度 |
void |
setAllCaps(boolean allCaps) |
|
void |
setKeyListener(KeyListener input) |
爲控件添加一個監聽器,監聽事件爲鍵盤按下。當作用於虛擬鍵盤時有重要而微妙的不同 |
void |
setPadding(int left,int top,int right,int bottom) |
設置內邊距 |
void |
setText(ChatSequence text) |
設置TextView顯示的文字 |