Buttons

IN THIS DOCUMENT

  1. Responding to Click Events
    1. Using an OnClickListener
  2. Styling Your Button
    1. Borderless button
    2. Custom background

KEY CLASSES

  1. Button
  2. ImageButton

A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.

Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways:

  • With text, using theButtonclass:
    <Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/button_text"    ... />
  • With an icon, using theImageButtonclass:
    <ImageButton    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/button_icon"    ... />
  • With text and an icon, using theButtonclass with theandroid:drawableLeftattribute:
    <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

When the user clicks a button, theButtonobject receives an on-click event.

To define the click event handler for a button, add theandroid:onClickattribute to the<Button>element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. TheActivityhosting the layout must then implement the corresponding method.

For example, here's a layout with a button usingandroid: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" />

Within theActivitythat hosts this layout, the following method handles the click event:

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

The method you declare in theandroid:onClickattribute must have a signature exactly as shown above. Specifically, the method must:

  • Be public
  • Return void
  • Define aViewas its only parameter (this will be theViewthat was clicked)

Using an OnClickListener

You can also declare the click event handler pragmatically rather than in an XML layout. This might be necessary if you instantiate theButtonat runtime or you need to declare the click behavior in aFragmentsubclass.

To declare the event handler programmatically, create anView.OnClickListenerobject and assign it to the button by callingsetOnClickListener(View.OnClickListener). For example:

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

The appearance of your button (background image and font) may vary from one device to another, because devices by different manufacturers often have different default styles for input controls.

You can control exactly how your controls are styled using a theme that you apply to your entire application. For instance, to ensure that all devices running Android 4.0 and higher use the Holo theme in your app, declareandroid:theme="@android:style/Theme.Holo"in your manifest's<application>element. Also read the blog post,Holo Everywherefor information about using the Holo theme while supporting older devices.

To customize individual buttons with a different background, specify theandroid:backgroundattribute with a drawable or color resource. Alternatively, you can apply astylefor the button, which works in a manner similar to HTML styles to define multiple style properties such as the background, font, size, and others. For more information about applying styles, seeStyles and Themes.

Borderless button

One design that can be useful is a "borderless" button. Borderless buttons resemble basic buttons except that they have no borders or background but still change appearance during different states, such as when clicked.

To create a borderless button, apply theborderlessButtonStylestyle to the button. For example:

<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

If you want to truly redefine the appearance of your button, you can specify a custom background. Instead of supplying a simple bitmap or color, however, your background should be a state list resource that changes appearance depending on the button's current state.

You can define the state list in an XML file that defines three different images or colors to use for the different button states.

To create a state list drawable for your button background:

  1. Create three bitmaps for the button background that represent the default, pressed, and focused button states.

    To ensure that your images fit buttons of various sizes, create the bitmaps asNine-patchbitmaps.

  2. Place the bitmaps into theres/drawable/directory of your project. Be sure each bitmap is named properly to reflect the button state that they each represent, such asbutton_default.9.png,button_pressed.9.png, andbutton_focused.9.png.
  3. Create a new XML file in theres/drawable/directory (name it something likebutton_custom.xml). Insert the following 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>

    This defines a single drawable resource, which will change its image based on the current state of the button.

    • The first<item>defines the bitmap to use when the button is pressed (activated).
    • The second<item>defines the bitmap to use when the button is focused (when the button is highlighted using the trackball or directional pad).
    • The third<item>defines the bitmap to use when the button is in the default state (it's neither pressed nor focused).

    Note:The order of the<item>elements is important. When this drawable is referenced, the<item>elements are traversed in-order to determine which one is appropriate for the current button state. Because the default bitmap is last, it is only applied when the conditionsandroid:state_pressedandandroid:state_focusedhave both evaluated as false.

    This XML file now represents a single drawable resource and when referenced by aButtonfor its background, the image displayed will change based on these three states.

  4. Then simply apply the drawable XML file as the button background:
    <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"  />

For more information about this XML syntax, including how to define a disabled, hovered, or other button states, read aboutState List Drawable.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. AI笔试面试题库-Python题目解析1
  2. 面试到底面什么?创优视觉科技有限公司告诉
  3. 10道Python题,快来看看你的基础怎么样?
  4. VRRP技术技术介绍及华为资料分享!
  5. 关于机器学习的知识点,全在这篇文章里了
  6. 什么是机器学习?有哪些分类?到底有什么用?终
  7. 从游戏AI到自动驾驶,一文看懂强化学习的概
  8. Python代码实操:详解数据清洗
  9. AER强调计量方法的重要性, 经济学因果分
  10. 迎战SDR、EW应用: Curtiss-Wright推出专用