[安卓API guides翻译]5.3.1 Buttons

按钮由文本或图标组成(或者两者兼有),并通过它们告诉用户按钮的功能。

根据你想要你的Button带文字,图片或者两者都有,有三种创建Button的方式:

  只有文字,使用Button类:

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

 

  只有图片,使用ImageButton类:

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

 

  既带文字又带图片,使用Button类并使用android:drawableLeft属性:

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

 

响应点击事件

当用户点击按钮,Button对象收到一个on-click事件。

 

要为按钮定义事件处理,则要在XML layout文件中为<Button>元素添加android:onclick属性。该属性的值必须是你想要调用的反映点击事件的方法名称。使用该布局的Activity则必须实现该方法。

 

例如,下面是一个layout,该layout中有一个button,并使用了android:onClick属性。

<?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" />

在使用该layout的Activity中,下面的方法将处理点击事件:

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

在android:onClick属性中声明的方法必须和上面展示的方法名一模一样,并且,该方法必须满足:

  公共权限(public)

  无返回值(void)

  定义一个View作为其唯一参数(该View将是被点击的组件)

 

使用点击监听器(OnClickListener)

 

你也可以不在XML layout中定义你的点击事件处理。

 

要用代码方式声明事件处理,需要创建View.OnClickListener对象并通过调用setOnClickListener(View.OnClickListener)来把它绑定在button上。例如:

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

 

按钮的外观(背景图片和字体)可能随设备不同而有所改变,这是因为不同制造商生产的设备会有不同的默认风格。

 

你可以通过声明整个app的主题(theme)来精确控制你的组件的风格。例如,为了让你的app在所有android4.0及以上的设备运行时使用Holo theme,需要在你的manifest文件的<application>元素中声明android:theme="@android:style/Theme.Holo"。如果想了解如何在旧版本设备上使用Holo theme的信息,请阅读博客Holo Everywhere。

 

如果想给个别按钮自定义背景,在android:background属性中指定drawable或color资源。你也可以为button绑定style,与HTML的工作方式类似,这可以让你定义许多特性,如背景,字体,大小等等。

 

Borderless button

“无边界”按钮是一种使用的设计。Borderless button与普通button类似,只是没有边界或背景,但依然在不同的状态下改变外观,例如被点击的时候。

 

要创建borderless button,为button使用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" />

自定义背景

如果想完全重新定义button的外观,可以指定自定义背景。这并非是提供一张简单的bitmap或者一种color,而应该是一个状态列表资源,当button当前状态变化时改变button的外观。

 

可以在XML文件中定义状态列表,它可以为用户定义三种不同的图片或颜色来区分button的不同状态。

 

要为button背景创建drawable状态列表,则:

1.创建三张bitmap作为button默认,按下,以及获得焦点三种状态的背景。要确保这些图片能适应不同的尺寸,可以创建Nine-patch bitmap。

2.将这些bitmap放入工程的res/drawable/目录下。每个bitmap的名字最好能正确反映button的状态,例如button_default.9.png。

3.在res/drawable/目录下新建XML文件(文件名如button_custom.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>

以上代码定义了一份drawable资源,根据button的当前状态可以改变背景image。

注意:<item>元素的顺序十分重要。当drawable被引用,会按顺序检验每个<item>元素是否符合当前button的状态。由于default bitmap在最后,当且仅当android:state_pressed和android:state_focused两个条件都不成立时才被使用。

该XML文件现在代表一份独立的drawable资源,当button引用该资源作为其背景,背景会根据button的三种状态而改变。

4.接下来只需要把drawable XML文件作为button的背景:

<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"  />

更多关于XML的语法,包括如何定义disabled,hovered,或其他button的状态,请阅读State List Drawable.

 

posted @ 2014-03-08 10:05  沙发土豆  阅读(147)  评论(0)    收藏  举报