先來整理一下TexView,EditView的用法。
Textview是最基本的組件,直接繼承了View,也是眾多組件的父類,所以了解她的屬性會對學習其他組件很有幫助。
TextView的屬性:
android:autoLink ? 設置是否當文本為URL鏈接/email/電話號碼/map時,文本顯示為可點擊的鏈接。可選值(none/web/email/phone/map/all)
android:autoText如果設置,將自動執行輸入值的拼寫糾正。此處無效果,在顯示輸入法并輸入的時候起作用。
android:bufferType指定getText()方式取得的文本類別。選項editable 類似于StringBuilder可追加字符, 也就是說getText后可調用append方法設置文本內容。spannable 則可在給定的字符區域使用樣式,參見這里1、這里2。
android:capitalize設置英文字母大寫類型。此處無效果,需要彈出輸入法才能看得到,參見EditView此屬性說明。 none不轉換,sentence每個句子的首字母大寫,words每個單詞字母大寫,characters每個字母都大寫
android:cursorVisible設定光標為顯示/隱藏,默認顯示。
android:digits設置允許輸入哪些字符。如“1234567890.+-*/% ()”?
**設置圖片和文本的位置關系:** android:drawableBottom在text的下方輸出一個drawable,如圖片。如果指定一個顏色的話會把text的背景設為該顏色,并且同時和background使用時覆蓋后者。
android:drawableLeft在text的左邊輸出一個drawable,如圖片。
android:drawablePadding設置text與drawable(圖片)的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可設置為負數,單獨使用沒有效果。
android:drawableRight在text的右邊輸出一個drawable。
android:drawableTop在text的正上方輸出一個drawable。
android:editable設置是否可編輯。
android:editorExtras設置文本的額外的輸入數據。
android:ellipsize設置當文字過長時,該控件該如何顯示。有如下值設置:”start”—?省略號顯示在開頭;”end”——省略號顯示在結尾;”middle”—-省略號顯示在中間; ”marquee” ——以跑馬燈的方式顯示(動畫橫向移動)
android:freezesText設置保存文本的內容以及光標的位置。
android:gravity設置文本位置,是指控件中文本的位置,區分layout_gravity屬性,如設置成“center”,文本將居中顯示。
android:hintText為空時顯示的文字提示信息,可通過textColorHint設置提示信息的顏色。此屬性在EditView中使用,但是這里也可以用。
android:imeOptions?附加功能,設置右下角IME動作與編輯框相關的動作,如actionDone右下角將顯示一個“完成”,而不設置默認是一個回車符號。這個在EditView中再詳細說明,此處無用。
android:imeActionId設置IME動作ID。
android:imeActionLabel設置IME動作標簽。
android:includeFontPadding設置文本是否包含頂部和底部額外空白,默認為true。
android:inputMethod為文本指定輸入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是這里報錯找不到。
android:inputType設置文本的類型,用于幫助輸入法顯示合適的鍵盤類型。在EditView中再詳細說明,這里無效果。
android:linksClickable設置鏈接是否點擊連接,即使設置了autoLink。
android:marqueeRepeatLimit在ellipsize指定marquee的情況下,設置重復滾動的次數,當設置為marquee_forever時表示無限次。
android:ems設置TextView的寬度為N個字符的寬度。這里測試為一個漢字字符寬度
android:maxEms設置TextView的寬度為最長為N個字符的寬度。與ems同時使用時覆蓋ems選項。
android:minEms設置TextView的寬度為最短為N個字符的寬度。與ems同時使用時覆蓋ems選項。
android:maxLength限制顯示的文本長度,超出部分不顯示。
android:lines設置文本的行數,設置兩行就顯示兩行,即使第二行沒有數據。
android:maxLines設置文本的最大顯示行數,與width或者layout_width結合使用,超出部分自動換行,超出行數將不顯示。
android:minLines設置文本的最小行數,與lines類似。
android:lineSpacingExtra設置行間距。
android:lineSpacingMultiplier設置行間距的倍數。如”1.2”
android:numeric如果被設置,該TextView有一個數字輸入法。此處無用,設置后唯一效果是TextView有點擊效果,此屬性在EdtiView將詳細說明。
android:password以小點”.”顯示文本
android:phoneNumber設置為電話號碼的輸入方式。
android:privateImeOptions設置輸入法選項,此處無用,在EditText將進一步討論。
android:scrollHorizontally設置文本超出TextView的寬度的情況下,是否出現橫拉條。
android:selectAllOnFocus如果文本是可選擇的,讓他獲取焦點而不是將光標移動為文本的開始位置或者末尾位置。TextView中設置后無效果。
**設置文本陰影:**
android:shadowColor指定文本陰影的顏色,需要與shadowRadius一起使用。
android:shadowDx設置陰影橫向坐標開始位置。 android:shadowDy設置陰影縱向坐標開始位置。
android:shadowRadius設置陰影的半徑。設置為0.1就變成字體的顏色了,一般設置為3.0的效果比較好。
android:singleLine 設置單行顯示。如果和layout_width一起使用,當文本不能全部顯示時,后面用“…”來表示。如android:text="test_ singleLine "
android:singleLine="true" android:layout_width="20dp"將只顯示“t…”。如果不設置singleLine或者設置為false,文本將自動換行
android:text設置顯示文本.
android:textAppearance設置文字外觀。如“?android:attr/textAppearanceLargeInverse”這里引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的 外觀。可設置的值如下:
textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmal l/textAppearanceSmallInverse
android:textColor設置文本顏色
android:textColorHighlight被選中文字的底色,默認為藍色
android:textColorHint?設置提示信息文字的顏色,默認為灰色。與hint一起使用。
android:textColorLink文字鏈接的顏色.
android:textScaleX設置文字之間間隔,默認為1.0f。
android:textSize設置文字大小,推薦度量單位”sp”,如”15sp”
android:textStyle設置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 可以設置一個或多個,用“|”隔開
android:typeface設置文本字體,必須是以下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3]
android:height設置文本區域的高度,支持度量單位:px(像素)/dp/sp/in/mm(毫米)
android:maxHeight設置文本區域的最大高度
android:minHeight設置文本區域的最小高度
android:width設置文本區域的寬度,支持度量單位:px(像素)/dp/sp/in/mm(毫米),與layout_width的區別看這里[http://blog.csdn.net/tuke_tuke/article/details/50464882](http://blog.csdn.net/tuke_tuke/article/details/50464882)?
android:maxWidth設置文本區域的最大寬度
android:minWidth設置文本區域的最小寬度
在設置TexView屬性時,可以使用Graphical Layout圖形布局的右邊可以看到當前android版本API下的所有屬性,如圖


在右邊的屬性欄中還有該組件的父組件的屬性列表,設置方便。
例子:不同顏色字體帶鏈接的TexView
Main_Activity.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- 文本框結尾處和文本前處繪制圖片 -->
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_launcher"
android:drawableLeft="@drawable/ic_launcher"
android:text="我愛我的祖國"
android:textSize="20dp" />
<!-- 設置中間省略,所有字母大寫 -->
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我愛Contry我愛contry是嗎是啊啊我愛Contry我愛contry是嗎是啊啊"
android:ellipsize="middle"
android:textAllCaps="true"
android:singleLine="true"
/>
<!-- 對電話,郵件增加鏈接 -->
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="郵件是tuketuke@163.com,電話是15927007602"
android:autoLink="email|phone"
/>
<!-- 設置設置文字的大小顏色,陰影 -->
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="測試文字大小顏色陰影"
android:textColor="#00f"
android:textSize="18dp"
android:shadowColor="#00f"
android:shadowDx="10.0"
android:shadowDy="3.0"
android:shadowRadius="3.0"
/>
<!-- 測試密碼框-->
<TextView
android:id="@+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="郵件是tuketuke@163.com,電話是02725614445"
android:password="true"
/>
<!-- 測試可勾選textview -->
<CheckedTextView
android:id="@+id/textView6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="@drawable/ok"
android:text="可勾選文本" />
</LinearLayout>
~~~

例:圓角邊框,漸變背景的TextView
xml文件是:
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- 使用xml定義drawable對象作為背景 -->
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_border"
android:text="帶邊框的文本"
android:textSize="20dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_border2"
android:text="圓角邊框,漸變背景的文本"
android:textSize="20dp" />
</LinearLayout>
~~~
默認情況下TexView是不帶邊框的,如果想為TextView加邊框,可以設置一個背景drawable對象,使用xml定義Drawable對象
在res/drawable文件夾中可以有直接的drawable資源對象,如.jpg,.png等圖片資源,也有使用xml定義的drawable對象,xml 定義的drawable對象有五種:StateListDrawable(selector根元素),LayerDrawable(layer-list根元素),ShapeDrwable(shape根元素),AnimationDrawable(set根元素),xxx。詳細的介紹以后會寫文章的


這里的是shape根元素,可以定義形狀背景等,大約有下邊的5個元素。
bg_border.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 設置矩形的背景色 -->
<solid android:color="#0000"></solid>
<!-- 設置邊框,寬度和顏色 -->
<stroke android:width="4px" android:color="#f00"></stroke>
</shape>
~~~
bg_border2.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">"
<!-- 設置矩形的4個圓角的半徑 -->
<corners android:topLeftRadius="5px" android:topRightRadius="5px" android:bottomLeftRadius="5px" android:bottomRightRadius="5px"></corners>
<!-- 設置邊框,寬度和顏色 -->
<stroke android:width="4px" android:color="#f0f"></stroke>
<!-- 設置漸變色,使用線性類型的漸變色,紅色,綠色,藍色 -->
<gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="linear"></gradient>"
</shape>
~~~

EditView的用法:
EditView的最重要的屬性是inputType屬性,根據不同的值,調出不同的輸入鍵盤。
下面用例子說明,例子:用戶友好的輸入界面
xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用戶名:"
android:textSize="20dp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="請填寫登錄賬號"
android:selectAllOnFocus="true" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密 碼:"
android:textSize="20dp" />
<!-- android:inputType="numberPassword"表明只接受數字密碼 -->
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberPassword" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年 齡:"
android:textSize="20dp" />
<!-- android:inputType="number"表明是數字輸入框 -->
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生 日:"
android:textSize="20dp" />
<!-- android:inputType="date"表明是日期輸入框 -->
<EditText
android:id="@+id/editText4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電話號碼:"
android:textSize="20dp" />
<!-- android:inputType="phone"表明是電話號碼輸入框 -->
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:selectAllOnFocus="true" />
</TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注冊" />
</TableLayout>
~~~


其實圖形化設計的時候可以直接拖動組件就好了,android已經定制好了組件

- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite