Input Controls(輸入控件)

一 Buttons(按鈕)

按鈕由一個(gè)圖標(biāo)或者一組文字(或者兩者都有)組成, 它用來(lái)展現(xiàn)當(dāng)用戶點(diǎn)擊按鈕時(shí)所觸發(fā)的事件.

button-types.png

你可以根據(jù)自己的需求通過(guò)以下3種方式創(chuàng)建一個(gè)布局文件.

1. 文字

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    ... />

2. 圖標(biāo)

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button_icon"
    ... />

3. 圖標(biāo)和文字

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:drawableLeft="@drawable/button_icon"
    ... />

Responding to Click Events

當(dāng)用戶點(diǎn)擊按鈕時(shí), Button對(duì)象就會(huì)收到一個(gè) on-click 事件.設(shè)置點(diǎn)擊事件有如下兩種方式.

1. Using Attribute "onClick"

在<Button>標(biāo)簽的中添加 "android : onClick"屬性來(lái)為按鈕添加點(diǎn)擊事件處理程序.
該屬性的值必須是響應(yīng)按鈕點(diǎn)擊事件的方法名.
關(guān)聯(lián)這個(gè)布局文件的Activity必須實(shí)現(xiàn)該響應(yīng)事件方法.

設(shè)置點(diǎn)擊事件處理程序 :

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />

在Activity中實(shí)現(xiàn)點(diǎn)擊事件處理方法 :

/** Called when the user touches the button */
public void sendMessage(View view) {
    // Do something in response to button click
}

注意 : 點(diǎn)擊事件處理方法必須滿足以下條件 :

  1. Be public
  2. Return void
  3. Define a View as its only parameter(this vill be the View that was clicked).
2. Using an OnClickListener

你可以在程序中創(chuàng)建按鈕點(diǎn)擊事件處理程序,而不是通過(guò)在 XML 中添加屬性的方式. 在以下
兩種情況下這是最好的選擇

1. 在運(yùn)行時(shí)實(shí)例化一個(gè)Button對(duì)象.
2. 在 Fragment 的子類中聲明按鈕的點(diǎn)擊行為.

在程序中創(chuàng)建一個(gè)View.OnClickListener 對(duì)象,然后通過(guò)
setOnClickListener(View.OnClickListener)方法傳遞給Button對(duì)象.

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do something in response to button click
    }
});

Styling Your Button (設(shè)計(jì)Button樣式)

同一個(gè)Button在不同設(shè)備上所展現(xiàn)的樣式(背景圖片和字體)可能是不一樣的,原因就在于不同的
生產(chǎn)商設(shè)置的輸入控件默認(rèn)樣式是不一樣的.

你可以通過(guò)使用主題(theme)來(lái)準(zhǔn)確設(shè)置整個(gè)應(yīng)用程序中的控件樣式.通過(guò)指定
"android:background"屬性值(一個(gè)color或者drawable資源)可以實(shí)現(xiàn)自定義樣式按鈕
也可以使用樣式文件(style.xml)來(lái)設(shè)置Button背景.

Borderless button (無(wú)邊按鈕)

指定 "android:borderlessButtonStyle" 屬性.

<Button
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
    style="?android:attr/borderlessButtonStyle" />

Custom background (自定義背景)

你可以通過(guò)指定一個(gè)自定義的背景來(lái)重新設(shè)計(jì)Button的樣式.不再使用一個(gè)簡(jiǎn)單的Bitmap圖片
或者顏色, 而使用一個(gè)state list 資源文件,它可以根據(jù)不懂的狀態(tài)設(shè)置不同的Button樣式.

你可以在一個(gè)state list XML文件中定義三種不同的圖片或者顏色分別對(duì)應(yīng)Button的不同狀態(tài)
.
創(chuàng)建一個(gè) state list drawable 資源文件

  1. 定義三張Bitmap圖片作為Button背景,它們分別代表著B(niǎo)utton的 default、pressed 和 focused 三種狀態(tài).
  2. 將Bitmap圖片放到當(dāng)前項(xiàng)目的 : "res/drawable/" 路徑下.
  3. 在 "res/drawable/" 路徑下創(chuàng)建XML文件.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>

注意點(diǎn) :

<item> 標(biāo)簽的順序非常重要.當(dāng)drawable資源被引用的時(shí)候.會(huì)遍歷<item>標(biāo)簽來(lái)確定最
適合當(dāng)前狀態(tài)的<item>. 由于default Bitmap 是最后一個(gè)<item>因此只有當(dāng) pressed 和
focused 都為 false 時(shí)才會(huì)使用default <item>.
  1. 引用drawable資源文件.
<Button
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
    android:background="@drawable/button_custom"  />

二 Text Fields

三 CheckBoxes

CheckBox 允許用戶從一個(gè)集合中選擇一個(gè)或者多個(gè)選項(xiàng). 通常情況下, 你應(yīng)該以豎直列表的
形式展現(xiàn)待選集合.

checkboxes.png

Responding to Click Events

<?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">
    <CheckBox android:id="@+id/checkbox_meat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/meat"
        android:onClick="onCheckboxClicked"/>
    <CheckBox android:id="@+id/checkbox_cheese"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cheese"
        android:onClick="onCheckboxClicked"/>
</LinearLayout>
public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();
    
    // Check which checkbox was clicked
    switch(view.getId()) {
        case R.id.checkbox_meat:
            if (checked)
                // Put some meat on the sandwich
            else
                // Remove the meat
            break;
        case R.id.checkbox_cheese:
            if (checked)
                // Cheese me
            else
                // I'm lactose intolerant
            break;
        // TODO: Veggie sandwich
    }
}

技巧 : 使用setChecked(boolean) or toggle()來(lái)設(shè)置狀態(tài).

未完待續(xù)...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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