android学习笔记9 - button的状态变化,strings,styles文件简介

1.button状态:

button有以下状态,普通态,被按下态(pressed),被选中态(selected),处于焦点状态(focused),被点击状态(checked)等。button对这些状态的监听可以使用独立的xml实现,且一般放置在drawable文件下,比如设定一个button的背景图片在各种状态下显示不同的图片:

android:background="@drawable/button_style1"

这样系统就会调用drawable文件夹下面的button_style1.xml文件进行处理,该文件详细定义如下:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/consumeactivity_numpress" android:state_pressed="true"/>
    <item android:drawable="@drawable/consumeactivity_numon"/>
</selector></span>

这里定义了button在按下状态和普通状态分别显示不同的两张图片。其中的android:state_pressed="true"就是表示当按下状态时触发。如果是其他状态,进行适当修改即可。xml文件时排列越前面优先级越高,它会一次从开始的第一条进行判断,如果成立便不执行后面的状态显示,所以常态请放在最后一条,其他的显示变化放在其之前。

button的状态和触发条件如下:

1.普通状态,系统默认状态。

2.点击状态:android:state_pressed的状态,当button被轻轻点击一下,或者用户按住该button时的状态。

3.被选中状态:android:state_selected,触屏手机无法正常触发该状态。

4.处于焦点状态:android:state_focuse,触屏手机无法正常触发该状态。

5.被点击状态:android:state_checked,复项单选radiobutton中的单选才能触发的状态,一般button不具有该状态。

2.strings文件:

strings文件是values文件夹下的字符串定义文件。其中可以定义一些常用的字符串常量方便应用使用,比如应用的appname等字符串信息。

定义格式如下:

 <string name="app_name">按钮</string>

其中标签中的内容不需要加上引号。

使用方式如下:

xml: android:text="@string/app_name"

java:R.string.app_name

其中第一个string表示使用string文件,第二个app_name就表示使用的标签。

3.styles文件:

styles文件也是values文件夹下的文件,是用来定义各式各样的自定义风格,可以用于定义xml布局中相同的布局风格,也可以是自定义的dialog风格等等。

定义格式如下:

<style name="picturebutton">
    <item name="android:background">@drawable/button_style3</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>    
</style>
其中的第一个name的picturebutton就是该style的名字。

使用方式如下:

style="@style/picturebutton"

在java代码中:

R.style.dialog
以下是结合上面所诉综合的radiobutton测试:

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/t5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio" />

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/t5"
        android:orientation="horizontal" >

        <RadioButton
            style="@style/radiobutton"
            android:id="@+id/radiobutton1" />
        <RadioButton
            style="@style/radiobutton"
            android:id="@+id/radiobutton2" />
        <RadioButton
            style="@style/radiobutton"
            android:id="@+id/radiobutton3" />
    </RadioGroup>

</RelativeLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">按钮</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="radio">RadioButton测试:</string>
</resources>
styles.xml:

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

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    <style name="radiobutton">
        <item name="android:drawableTop">@drawable/button_style3</item>
        <item name="android:textColor">#555555</item>
        <item name="android:textSize">18dp</item>
        <item name="android:text">"综合"</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:layout_weight">1.0</item>
        <item name="android:layout_marginTop">5dip</item>
        <item name="android:layout_marginLeft">20dip</item>
        <item name="android:layout_marginRight">20dip</item>
        <item name="android:layout_marginBottom">5dip</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:button">@null</item>   <!-- 消去自带的radiobutton -->
    </style>

</resources>
button_style3.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/a3" 
          android:state_checked="true"/>
    <item android:drawable="@drawable/a4"/>
</selector>

点击后只有一个按钮处于被点击状态,点击其中一个按钮,其他按钮的状态自动恢复。效果如下:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值