TextVuew
TextView其實(shí)就是一個(gè)文本編輯器翰苫,只是Android關(guān)閉了它的文字編輯功能瞎暑。
TextView
直接繼承了View匕坯,與其子類的類圖關(guān)系:
# TextView常用的xml屬性及相關(guān)方法
xml屬性 | 相關(guān)方法 | 說(shuō)明 |
---|---|---|
android:autoLink | setAutoLinkMask(int) | 是否將符合指定格式的文本轉(zhuǎn)換為可單擊的超鏈接形式 |
android:autoText | setKeyLinstener(KeyLinstener) | 控制是否將的URL掠抬、email等地址自動(dòng)轉(zhuǎn)換為可單擊的鏈接 |
android:linksClickable | setLinksClickable(boolean) | 控制該文本框的URL氮块、email等鏈接是否具可以點(diǎn)擊 |
android:capitalize | setKeyLinstener(KeyLinstener) | 控制是否將用戶輸入的文本轉(zhuǎn)換為大寫(xiě)字母 |
android:cursorVisible | setCursorVisiblek(boolean) | 設(shè)置該文本框的光標(biāo)是否可見(jiàn) |
android:drawableBottom | setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) | 在文本框底端處繪制圖像 |
android:drawableTop | setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) | 在文本框頂端處繪制圖像 |
android:drawableEnd | setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) | 在文本框結(jié)尾處繪制圖像 |
android:drawableLeft | setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) | 在文本框左邊處繪制圖像 |
android:drawableRight | setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) | 在文本框右邊處繪制圖像 |
android:drawablePadding | setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) | 設(shè)置文本框內(nèi)文本與圖形的間距 |
android:drawableStart | setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) | 在文本框開(kāi)始處繪制圖像 |
android:editable | 設(shè)置文本框是否允許被編輯 | |
android:ellipsize | setEllipsize(TextUitls,TruncateAt) | 當(dāng)顯示文本超過(guò)了TextView的寬度時(shí)绍载,如何處理文本 |
android:ems | setEms(int) 設(shè)置文本框的寬度 | |
android:height | setHeight(int) | 設(shè)置文本框的高度 |
android:fontFamily | setTypeface(Typeface) | 設(shè)置文本框內(nèi)文本的字體 |
android:gravity | setGravity(int) | 設(shè)置文本框內(nèi)文本的對(duì)齊方式 |
android:hint | setHint(int) | 設(shè)置文本框內(nèi)容為空時(shí),文本框內(nèi)默認(rèn)顯示的提示文字 |
android:inputType | setRawInputType(int) | 設(shè)置文本框輸入方式 |
android:lines | setLines(int) | 設(shè)置文本框默認(rèn)占幾行 |
android:maxEms | setMaxEms(int) | 設(shè)置文本框最大寬度 |
android:maxHeight | setMaxHeight(int) | 設(shè)置文本框最大高度 |
android:maxLenghth | setMaxLenghth(int) | 設(shè)置文本框最大字符長(zhǎng)度 |
android:maxWidth | setMaxWidth(int) | 設(shè)置文本框最大寬度 |
android:maxLines | setMaxLines(int) | 設(shè)置文本框最多占幾行 |
android:maxEms | setMaxEms(int) | 設(shè)置文本框最小寬度 |
android:minHeight | setMinHeight(int) | 設(shè)置文本框最小高度 |
android:minLenghth | setMinxLenghth(int) | 設(shè)置文本框最小長(zhǎng)度 |
android:minWidth | setMinWidth(int) | 設(shè)置文本框最小寬度 |
android:minLines | setMinLines(int) | 設(shè)置文本框最少占幾行 |
android:passwordd | setTransformatinMethod(TransformatinMethod) | 設(shè)置文本框是一個(gè)密碼框 |
android:phoneNumber | setKeyListener(KeyListener) | 設(shè)置文本框只接受電話號(hào)碼 |
android:scrollHorizontally | setHorizontallyScrolling(boolean) | 設(shè)置文本框不夠顯示全部?jī)?nèi)容時(shí)是否允許水平滾動(dòng) |
android:selectAllOnFocus | setSelectAllOnFocus(boolean) | 如果文本框內(nèi)的內(nèi)容可選擇滔蝉,設(shè)置是否當(dāng)它獲得焦點(diǎn)時(shí)自動(dòng)選擇所有文本 |
android:shawdowColor | setshadowLayer(float,float,float,int) | 設(shè)置文本框內(nèi)文本的陰影的顏色 |
android:shawdowDx | setshadowLayer(float,float,float,int) | 設(shè)置文本框內(nèi)文本的陰影在水平方向的偏移 |
android:shawdowDy | setshadowLayer(float,float,float,int) | 設(shè)置文本框內(nèi)文本的陰影在垂直方向的偏移 |
android:shawdowRadius | setshadowLayer(float,float,float,int) | 設(shè)置文本框內(nèi)文本的陰影的模糊程度击儡,值越大,陰影越模糊 |
android:singleLine | setTransformationMethod() | 設(shè)置文本框是否為單行模式 |
android:text | setText(CharSequence) | 設(shè)置是否將文本框文本的內(nèi)容 |
android:textAllCaps | setAllCaps(boolean) | 設(shè)置是否將文本框中所有字幕顯示為大寫(xiě)字母 |
android:textAppearance | 設(shè)置文本框的顏色蝠引、字體曙痘、大小等樣式 | |
android:textColor | setTextColor(ColorStateList) | 設(shè)置文本框中文本的顏色 |
android:textColorHighlight | setHighlightColor(int) | 設(shè)置文本框被選中時(shí)的顏色 |
android:textColorHint | setHintTextColor(int) | 設(shè)置文本框中提示文本的顏色 |
android:textColorLink | setLinkTextColor(int) | 設(shè)置文本框中鏈接的顏色 |
android:textIsSelectable | setTextSelectedable() | 設(shè)置該文本框不能編輯時(shí),文本框內(nèi)的文本是否可以被選中 |
android:textSize | setTextSize(float) | 設(shè)置文本框的字體大小 |
android:textStyle | setTypeface(Typeface) | 設(shè)置文本框內(nèi)字體風(fēng)格立肘,如粗體边坤、斜體等 |
android:width | setWidth(int) | 設(shè)置該文本框的寬度 |
示例:不同顏色、字體谅年、帶鏈接的文本
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 設(shè)置字體為20pt茧痒,文本框結(jié)尾處繪制圖片 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我愛(ài)Java"
android:textSize="20pt"
android:drawableEnd="@drawable/ic_launcher"/>
<!-- 設(shè)置中間省略, 所有字母大寫(xiě) -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="我愛(ài)Java我愛(ài)Java我愛(ài)Java我愛(ài)Java我愛(ài)Java我aaaJava"
android:ellipsize="middle"
android:textAllCaps="true"/>
<!-- 對(duì)郵件融蹂、電話增加鏈接 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="郵件是kongyeeku@163.com旺订,電話是02088888888"
android:autoLink="email|phone"/>
<!-- 設(shè)置文字顏色 、大小超燃,并使用陰影 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="測(cè)試文字"
android:shadowColor="#0000ff"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:textColor="#f00"
android:textSize="18pt"/>
<!-- 測(cè)試密碼框 -->
<TextView android:id="@+id/passwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:password="true"/>
<!-- 測(cè)試CheckedTextView
通過(guò)checkMark設(shè)置該文本框的勾選圖標(biāo)
-->
<CheckedTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="可勾選的文本"
android:checkMark="@drawable/ok"/>
</LinearLayout>
EditText
EditText與TextView的最大區(qū)別在于:EditText可以接受用戶輸入区拳。
EditText組件最重要的屬性是inputType,用于EditText為指定類型的輸入組件意乓。
EditText還派生了如下兩個(gè)子類:
-
AutoCompleteTextView
:帶有自動(dòng)完成功能的EditText樱调,實(shí)際上該組件的命名不太恰當(dāng)。應(yīng)該叫AutoCompleteEditText
更合適届良。該類通常需要與Adapter
結(jié)合使用笆凌。 -
ExtractEditText
:它并不是UI組件,而是EditText
組件的底層服務(wù)類士葫,負(fù)責(zé)提供全屏輸入法支持乞而。
示例:用戶友好的輸入界面
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用戶名:"
android:textSize="16sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)?zhí)顚?xiě)登錄帳號(hào)"
android:selectAllOnFocus="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密碼:"
android:textSize="16sp"
/>
<!-- android:inputType="numberPassword"表明只能接收數(shù)字密碼 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="年齡:"
android:textSize="16sp"
/>
<!-- inputType="number"表明是數(shù)值輸入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生日:"
android:textSize="16sp"
/>
<!-- inputType="date"表明是日期輸入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="date"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="電話號(hào)碼:"
android:textSize="16sp"
/>
<!-- inputType="phone"表明是輸入電話號(hào)碼的輸入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)?zhí)顚?xiě)您的電話號(hào)碼"
android:selectAllOnFocus="true"
android:inputType="phone"
/>
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注冊(cè)"
/>
</TableLayout>
按鈕(Button)
Button繼承了TextView , 它主要是在UI界面上生成一個(gè)按鈕,該按鈕可以供用戶單擊慢显,當(dāng)用戶單擊按鈕時(shí)爪模, 按鈕會(huì)觸發(fā) 一個(gè)onClick事件欠啤。關(guān)于onClick事件編程的簡(jiǎn)單示例,本書(shū)前面己經(jīng)見(jiàn)到很多屋灌,后面還會(huì)詳細(xì)介紹Android的事件編程跪妥。
按鈕使用起來(lái)比較容易,可以通過(guò)為按鈕指定android:background屬性為按鈕增加背景顏色或背景圖片声滥,如果將背景圖片設(shè)為不規(guī)則的背景圖片,則可以開(kāi)發(fā)出各種不規(guī)則形狀的按鈕侦香。
如果只是使用普通的背景顏色或背景圖片落塑,那么這些背景是固定的,不會(huì)隨著用戶的動(dòng)作而改變罐韩。如果需要讓按鈕的背景 顏 色憾赁、背景圖片隨用戶動(dòng)作動(dòng)態(tài)改變,則可以考慮使用自定義Drawable對(duì)象來(lái)實(shí)現(xiàn)散吵。
示例:按鈕龙考、圓形按鈕、帶文字的圖片按鈕
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 文字帶陰影的按鈕 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文字帶陰影的按鈕"
android:textSize="12pt"
android:shadowColor="#aa5"
android:shadowRadius="1"
android:shadowDx="5"
android:shadowDy="5"
/>
<!-- 普通文字按鈕 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/red"
android:text="普通按鈕"
android:textSize="10pt"
/>
<!-- 帶文字的圖片按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:textSize="11px"
android:text="帶文字的圖片按鈕"
/>
</LinearLayout>
第三個(gè)按鈕有點(diǎn)特殊矾睦,它指定了 android:background屬性為@drawable/button_selector晦款,該屬性值引用一個(gè)Drawable資源,該資源對(duì)應(yīng)的XML文件如:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 指定按鈕按鈕下時(shí)的圖片 -->
<item android:state_pressed="true"
android:drawable="@drawable/red"
/>
<!-- 指定按鈕松開(kāi)時(shí)的圖片 -->
<item android:state_pressed="false"
android:drawable="@drawable/purple"
/>
</selector>
使用9Patch圖片作為按鈕背景
9Patch圖片是一種特殊的PNG圖片枚冗,這種圖片以 .9.png結(jié)尾缓溅,它在原始圖片四周各添加一個(gè)寬度為1像素的線條,這4條線就決定了該圖片的縮放規(guī)則赁温、內(nèi)容昆示規(guī)則坛怪。
左側(cè)和上側(cè)的直線共同決定了圖片的縮放區(qū)域:以左邊直線為左邊界繪制矩形,它覆蓋的區(qū)域可以在縱向縮放股囊; 以上面直 線為上邊界繪制矩形袜匿,它覆蓋的區(qū)域可以水平縮 放; 它們二者的交集可以在兩個(gè)方向上縮放稚疹。
右側(cè)和下側(cè)的直線共同決定圖片的內(nèi)容顯示區(qū)域: 以右邊直線為右邊界繪制矩形居灯,以下邊直線為下邊界繪制矩形,它們二者的交集就是圖片的內(nèi)容顯示區(qū)域 内狗。
Android為制作9Patch圖片提供了 draw9patch工具穆壕,該工具位于Android SDK安裝路徑的tools目錄下,進(jìn)入該目錄雙擊draw9patch.bat文件其屏,即可啟動(dòng)該工具喇勋。
單選按鈕(RadioButton)與復(fù)選框(CheckBox)
RadioButton、CheckBox與普通按鈕不同的是偎行,它們多了一個(gè)可選中的功能川背,因此RadioButton贰拿、CheckBox都可額外指定一個(gè)android:checked屬性,該屬性用于指定RadioButton熄云、CheckBox初始時(shí)是否被選中膨更。
RadioButton與CheckBox的不同之處在于,一組RadioButton只能選中其中一個(gè)缴允,因此RadioButton通常要與RadioGroup—起使用荚守,用于定義一組單選按鈕。
實(shí)例:利用單選按鈕练般、愛(ài)選框獲取用戶信息
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別:"
android:textSize="16px"/>
<!-- 定義一組單選框 -->
<RadioGroup android:id="@+id/rg"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<!-- 定義兩個(gè)單選框 -->
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="男"
android:checked="true"/>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="女"/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜歡的顏色:"
android:textSize="16px"
/>
<!-- 定義一個(gè)垂直的線性布局 -->
<LinearLayout
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- 定義三個(gè)復(fù)選框 -->
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="紅色"
android:checked="true"/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="藍(lán)色"/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綠色"/>
</LinearLayout>
</TableRow>
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableLayout>
如果在XML布局文件中默認(rèn)勾選了某個(gè)單選按鈕矗漾,則必須為該組單選按鈕的每個(gè)按鈕指定android:id屬性值,否則這組單選按鈕不能正常工作.
為了監(jiān)聽(tīng)單選按鈕薄料、復(fù)選框的勾選狀態(tài)的改變敞贡,可以為它們添加事件監(jiān)聽(tīng)器。例如下面Activity為RadioGroup添加了事件監(jiān)聽(tīng)器摄职,該監(jiān)聽(tīng)器可以監(jiān)聽(tīng)這組單選按鈕的勾選狀態(tài)的改變誊役。
package org.crazyit.ui;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
/**
* Description:
* <br/>site: <a >crazyit.org</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class CheckButtonTest extends Activity
{
RadioGroup rg;
TextView show;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲取界面上rg、show兩個(gè)組件
rg = (RadioGroup) findViewById(R.id.rg);
show = (TextView) findViewById(R.id.show);
// 為RadioGroup組件的OnCheck事件綁定事件監(jiān)聽(tīng)器
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// 根據(jù)用戶勾選的單選按鈕來(lái)動(dòng)態(tài)改變tip字符串的值
String tip = checkedId == R.id.male ?
"您的性別是男人": "您的性別是女人";
// 修改show組件中的文本谷市。
show.setText(tip);
}
});
}
}
狀態(tài)開(kāi)關(guān)按鈕(ToggleButton)與開(kāi)關(guān)(Switch)
狀態(tài)開(kāi)關(guān)按鈕(ToggleButton)與開(kāi)關(guān)(Switch)也是由Button派生出來(lái)的蛔垢,因此它們的本質(zhì)也是按鈕,Button支持的各種屬性迫悠、方法也適用于ToggleButton和Switch啦桌。從功能上來(lái)看,ToggleButton及皂、Switch與CheckBox復(fù)選框非常相似甫男,它們都可以提供兩個(gè)狀態(tài)。不過(guò)ToggleButton验烧、Switch與CheckBox的區(qū)別主要體現(xiàn)在功能上板驳,ToggleButton、Switch通常用于切換程序中的某種狀態(tài)碍拆。
# ToggleButton支持的XML屬性及相關(guān)方法說(shuō)明
XML屬性 | 相關(guān)方法 | 說(shuō)明 |
---|---|---|
android:checked | setChecked(boolean) | 設(shè)置該按鈕是否被選中 |
android:textOff | 設(shè)置當(dāng)該按鈕的狀態(tài)關(guān)閉時(shí)顯示的文本 | |
android:textOn | 設(shè)置當(dāng)該按鈕的狀態(tài)打開(kāi)時(shí)顯示的文本 |
# Switch支持的XML屬性及相關(guān)方法說(shuō)明
XML屬性 | 相關(guān)方法 | 說(shuō)明 |
---|---|---|
android:checked | setChecked(boolean) | 設(shè)置該開(kāi)關(guān)是否被選中 |
android:switchMinWidth | setSwitchMinWidth(int) | 設(shè)置該開(kāi)關(guān)的最小寬度 |
android"switchPadding | setSwitchPadding(int) | 設(shè)置開(kāi)關(guān)與標(biāo)題文本之間的空白 |
android:switchTextAppearance | setSwitchTextAppearance(Context,int) | 設(shè)置該開(kāi)關(guān)圖標(biāo)上的文本樣式 |
android:textOff | setTextOff(CharSequence) | 設(shè)置該開(kāi)關(guān)的狀態(tài)關(guān)閉時(shí)顯示的文本 |
android:textOn | setTextIOn(CharSequence) | 設(shè)置該開(kāi)關(guān)的狀態(tài)打開(kāi)時(shí)顯示的文本 |
android:textStyle | setSwitchTypeface(Typeface) | 設(shè)置該開(kāi)關(guān)的文本的風(fēng)格 |
android:thumb | setThumbResource(int) | 指定使用自定義Drawable繪制該開(kāi)關(guān)的開(kāi)關(guān)按鈕 |
android:track | setThumbResource(int) | 指定使用自定義Drawable繪制該開(kāi)關(guān)的開(kāi)關(guān)軌道 |
android:typeface | setSwitchTypeface(Typeface) | 設(shè)置該開(kāi)關(guān)的文本的字體風(fēng)格 |
實(shí)例:動(dòng)態(tài)控制布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 定義一個(gè)ToggleButton按鈕 -->
<ToggleButton android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="橫向排列"
android:textOn="縱向排列"
android:checked="true"/>
<Switch android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="橫向排列"
android:textOn="縱向排列"
android:thumb="@drawable/check"
android:checked="true"/>
<!-- 定義一個(gè)可以動(dòng)態(tài)改變方向的線性布局 -->
<LinearLayout android:id="@+id/test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測(cè)試按鈕一"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測(cè)試按鈕二"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測(cè)試按鈕三"/>
</LinearLayout>
</LinearLayout>
接下來(lái)我們?yōu)門oggleButton按鈕若治、Switch按鈕綁定監(jiān)聽(tīng)器,當(dāng)它的選中狀態(tài)發(fā)生改變時(shí)感混,程序通過(guò)代碼來(lái)改變LinearLayout的布局方向端幼。
package org.crazyit.ui;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;
/**
* Description:
* <br/>site: <a >crazyit.org</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class ToggleButtonTest extends Activity
{
ToggleButton toggle;
Switch switcher;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton)findViewById(R.id.toggle);
switcher = (Switch)findViewById(R.id.switcher);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
OnCheckedChangeListener listener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton button
, boolean isChecked)
{
if(isChecked)
{
//設(shè)置LinearLayout垂直布局
test.setOrientation(1);
}
else
{
//設(shè)置LinearLayout水平布局
test.setOrientation(0);
}
}
};
toggle.setOnCheckedChangeListener(listener);
switcher.setOnCheckedChangeListener(listener);
}
}
時(shí)鐘(AnalogClock)與DigitalClock
時(shí)鐘UI組件是兩個(gè)非常簡(jiǎn)單的組件,DigitalClock本身就繼承了TextView——也就是說(shuō)它本身就是文本框弧满,只是它里面顯示的內(nèi)容總是當(dāng)前時(shí)間婆跑。與TextView不同的是,為DigitalClock設(shè)置android:text屬性沒(méi)什么作用庭呜。
AnalogClock則繼承了View組件滑进,它重寫(xiě)了View的OnDraw方法犀忱,它會(huì)在View上繪制模擬時(shí)鐘。
# AnalogClock支持的XML屬性的說(shuō)明
XML屬性 | 說(shuō)明 |
---|---|
android:dial | 設(shè)置該模擬時(shí)鐘的表盤使用的圖片 |
android:hand_hour | 設(shè)置該模擬時(shí)鐘的時(shí)針表盤使用的圖片 |
android:hand_minute | 設(shè)置該模擬時(shí)鐘的分針使用的圖片 |
DigitalClock和AnalogClock都會(huì)顯示當(dāng)前時(shí)間扶关,不同的是阴汇,DigitalClock顯示數(shù)字時(shí)鐘,可以顯示當(dāng)前的秒數(shù):AnalogClock顯示模擬時(shí)鐘节槐,不會(huì)顯示當(dāng)前秒數(shù)搀庶。
實(shí)例:手機(jī)里的“勞力士”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<!-- 定義模擬時(shí)鐘 -->
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- 定義數(shù)字時(shí)鐘 -->
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14pt"
android:textColor="#f0f"
android:drawableRight="@drawable/ic_launcher"/>
<!-- 定義模擬時(shí)鐘,并使用自定義表盤、時(shí)針圖片 -->
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dial="@drawable/watch"
android:hand_minute="@drawable/hand"/>
</LinearLayout>
計(jì)時(shí)器(Chornometer)
Android還提供了一個(gè)計(jì)時(shí)器組件:Chronometer铜异,該組件與DigitalClock都繼承自TextView,因此它們都會(huì)顯示一段文本哥倔。但Chronometer并不顯示當(dāng)前時(shí)間,它顯示的是從某個(gè)起始
時(shí)間開(kāi)始熙掺,一共過(guò)去了多長(zhǎng)時(shí)間。
Chronometer的用法也很簡(jiǎn)單咕宿,它只提供了一個(gè)android:format屬性币绩,用于指定計(jì)時(shí)器的計(jì)時(shí)格式。除此之外府阀,Chronometer支持如卞常用方法缆镣。
-
setBase(longbase)
: 設(shè)置計(jì)時(shí)器的起始時(shí)間。 -
setFormat(Stringformat)
:設(shè)置M示時(shí)間的格式试浙。 -
start()
:開(kāi)始計(jì)時(shí)董瞻, -
stop()
:停止計(jì)時(shí)。 -
setOnChronometerTickListener(Chronometer.OnChronometerTickListenerlistener)
:為計(jì)時(shí)器綁定事件監(jiān)聽(tīng)器田巴,當(dāng)計(jì)時(shí)器改變時(shí)觸發(fā)該監(jiān)聽(tīng)器钠糊。
實(shí)例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<Chronometer
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12pt"
android:textColor="#ffff0000"/>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啟動(dòng)"/>
</LinearLayout>