Android之Button

AndroidButton

序言:此文参照官方网站:

http://developer.android.com/guide/topics/ui/controls/button.html

撰写,部分样例代码引用官方代码。样式在本文中只做了一个简单的叙述,详细的论述将继续探讨。

一、创建Button的方法

方式一(Button中只显示文字提示):

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

方式二(Button中只显示背景图片):

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

方式三(Button中文字和图片都显示):

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

在这里通过属性:android:drawableLeft使得图片是显示在文字的左方的。也可以通过属性:android:drawableTopdrawableBottomdrawableRight显示在图片显示在文字的上方、下方及右方。

注:创建无边Button,在<Button/>元素中添加属性:style="?android:attr/borderlessButtonStyle" 可以实现。

二、添加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" />

通过属性:android:onClick="方法名"实现。但必须注意此方法必须在调用该样式文件的Activity中定义,定义的实现如下:

public void sendMessage(View view) {
    // Do something in response to button click
}

此方法中有三个地方必须注意:

1、方法必须是public

2、方法的返回值必须是void

3、方法的参数有且仅有一个,而且必须是View类型的。

方式二(在运动时声明Button或者在Fragment的子类中声明):

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
    }
});

三、Button自定义样式

可以自定义文本显示的大小、颜色、字体及Button的背景等。

方式一(在<Button/>元素中直接定义Button的样式):

<Button

    android:layout_width="wrap_content"

        android:layout_height="wrap_content"

    android:textColor="#00FF00"

    android:textSize="12sp"

    android:textStyle="bold"

    android:text="@string/hello_world"

    android:background="@android:drawable/btn_dialog"/>

方式二(在单独的样式文件中定义):

<Button

    android:layout_width="wrap_content"

        android:layout_height="wrap_content"

    style="@style/CodeStyle"

    android:text="@string/hello_world"/>

在工程目录res/values/下定义样式文件,如:style_name.xmlstyle_name可以任意定义,只要符合文件命名规则就行,如:custom_style.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="CodeStyle" parent="android:Theme.Light">

        <item name="android:textColor">#00FF00</item>

        <item name="android:textSize">12sp</item>

        <item name="android:textStyle">italic</item>

        <item name="android:background">@android:drawable/btn_dialog</item>

    </style>

</resources>

当然也可以在运行时动态通过对应的方法设置显示属性。

四、Button对于不同的动作显示不同的背景

步骤一(在res/drawable/目录下新建文件,此例文件名为: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>

步骤二(在<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"  />

注:<selector/>元素的定义语法参照如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" "false"]
    android:dither=["true" "false"]
    android:variablePadding=["true" "false">
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" "false"]
        android:state_focused=["true" "false"]
        android:state_hovered=["true" "false"]
        android:state_selected=["true" "false"]
        android:state_checkable=["true" "false"]
        android:state_checked=["true" "false"]
        android:state_enabled=["true" "false"]
        android:state_activated=["true" "false"]
        android:state_window_focused=["true" "false"/>
</selector>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值