按钮包含了用来指示按下去会发生什么的图标和文字。
根据你时候需要显示文字,图标或者二者都要,你可以用下面三种方式来创建你写的布局。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
... />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_icon"
... />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:drawableLeft="@drawable/button_icon"
... />
当用户点击一个按钮的时候,Button对象就接到一个点击事件。
想要定义你的button的点击事件的handler,就要在xml文件的<Button>
属性下面添加android:onClick属性。
这个属性的值一定要是你的button被点击时候,你想要响应而调用的函数。这个布局的宿主Activity必须要实现对应的方法。
例如,下面是一个包含使用了android:onClick属性的button的布局的例子:
<?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当中,下面的方法处理点击事件:
/** 当用户点击按钮的时候调用 */
public void sendMessage(View view) {
// Do something in response to button click
}
你在android:onClick中声明的属性必须要有一个准确的标识,如同上面显示的那样。特别是这个方法一定要是:
公有 public
返回值为空 void
定义一个view作为唯一的传入参数。(view应该是点击的那个view)
你也可以通过编程来实现一个点击事件handler,而不使用xml文件中指定属性的方法。如果你想要在运行时实例化一个Button或者你需要在Fragment子类声明点击事件的时候,这么做事必要的。
通过编程声明点击事件,你要创建一个View.OnClickListener对象并且把它赋给button,通过调用setOnClickListener(View.OnClickListener).例如:
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
}
});
你的按钮的外观(背景图片和字体)可能在不同设备之间不尽相同,因为不同厂家定制的设备可能拥有不同的默认的输入通知的定制类型。
你可以使用整个应用的主题来精确地控制你的控件(controls)的风格。例如,为了保证所有android4.0以及以上的设备都可以在你的app中使用holo主题,在manifest的<application>
元素中声明android:theme="@android:style/Theme.Holo"属性。并且参考博客帖子Holo Everwhere来获取更多的关于支持更多旧设备的信息。
定制每一个button的不同的background风格,使用drawable或者color资源来指定一个andorid:background属性。还用一种方式就是你可以指定一个button的style属性,它用一种类似于html风格的方式来定义多种的风格的属性,比如说backgroudn, font ,size等等。更多的关于应用style的信息,请参考Styles and Themes文章。
还用一种很有用的 设计风格就是无边界button。无边界button组合了基本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的外观,你可以指定一个定制的background。不再仅仅是提供一个位图或者颜色,你的背景应该是一个state list 资源,根据按钮的当前不同状态来改变按钮的外观。
你可以在XML文件中定义state list来指定按钮的不同状态下要显示的资源。
创建button背景的state list:
为按钮的三个状态创建三个不同的位图,分别是 default, pressed , focused三种。
为了保证你的位图资源可以适应不同大小的button,把位图资源应该是 nine-patch位图。
把位图放进你项目的res/drawable/文件夹下面。保证每一个位图的命名都恰当地反应了它们代表的状态,比如说button_default.9.png,button_pressed.9.png,以及button_focused.9.png.
在/res/drawable文件夹下面创建新的xml文件(命名为比如说 button_custom.xml)。写入如下的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资源,它会根据当前的按钮的状态来改变它的图片。
第一个<item>
定义了当button按下时候显示的图片。
第二个<item>
定义了当的这个按钮获得焦点的时候显示的图片(当使用轨迹球或者方向导航键的时候按钮被高亮的时候)
第三个<item>
定义了这个button默认情况的显示,(既没有按下,也没有获得焦点)
提示:
<item>
标签的顺序很重要。当引用drawable资源的时候,元素按照顺序被遍历来决定哪一个适合当前的button状态。因为默认的位图是最后一个,所以只有当判断 Android:state_pressed 和 android;state_focused的值都是false的时候才会启用。
这个xml对象提供了一个单独的drawable资源,并且被一个button的background引用。图片的显示会基于三种不同的状态来现实。
下面是对于这种drawable资源作为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语法的信息,包括怎样定义一个禁用的或者悬停的以及其他的button状态,请参考State List Drawable文档。