Android精講--界面編程3

TextVuew


TextView其實(shí)就是一個(gè)文本編輯器翰苫,只是Android關(guān)閉了它的文字編輯功能瞎暑。

TextView直接繼承了View匕坯,與其子類的類圖關(guān)系:

TextView及其子類的類圖

# 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ū)域 内狗。

圖片縮放區(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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市壹哺,隨后出現(xiàn)的幾起案子抄伍,更是在濱河造成了極大的恐慌,老刑警劉巖管宵,帶你破解...
    沈念sama閱讀 219,110評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件截珍,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡箩朴,警方通過(guò)查閱死者的電腦和手機(jī)岗喉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)炸庞,“玉大人钱床,你說(shuō)我怎么就攤上這事〔壕樱” “怎么了诞丽?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,474評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵鲸拥,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我僧免,道長(zhǎng)刑赶,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,881評(píng)論 1 295
  • 正文 為了忘掉前任懂衩,我火速辦了婚禮撞叨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘浊洞。我一直安慰自己牵敷,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布法希。 她就那樣靜靜地躺著枷餐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪苫亦。 梳的紋絲不亂的頭發(fā)上毛肋,一...
    開(kāi)封第一講書(shū)人閱讀 51,698評(píng)論 1 305
  • 那天屋剑,我揣著相機(jī)與錄音润匙,去河邊找鬼。 笑死唉匾,一個(gè)胖子當(dāng)著我的面吹牛孕讳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播巍膘,決...
    沈念sama閱讀 40,418評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼厂财,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了峡懈?” 一聲冷哼從身側(cè)響起蟀苛,我...
    開(kāi)封第一講書(shū)人閱讀 39,332評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎逮诲,沒(méi)想到半個(gè)月后帜平,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡梅鹦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評(píng)論 3 337
  • 正文 我和宋清朗相戀三年裆甩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片齐唆。...
    茶點(diǎn)故事閱讀 40,110評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嗤栓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情茉帅,我是刑警寧澤叨叙,帶...
    沈念sama閱讀 35,792評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站堪澎,受9級(jí)特大地震影響擂错,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜樱蛤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評(píng)論 3 331
  • 文/蒙蒙 一钮呀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧昨凡,春花似錦爽醋、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,003評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至哪痰,卻和暖如春遂赠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背妒御。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,130評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工解愤, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留镇饺,地道東北人乎莉。 一個(gè)月前我還...
    沈念sama閱讀 48,348評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像奸笤,于是被迫代替她去往敵國(guó)和親惋啃。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容