• selector的button选中处理问题


    1、背景介绍

           在做Android项目开发的时候,有时我们须要对button做一些特殊的处理,比方button点击的时候会有一个动画的效果,实际上就是几张图片在短时间的切换。再比方有时候我们须要对界面的选择的时候,须要表示该项目是选中状态还是不被选中状态。怎么办,相同使用两张图片。分别表示不同状态即可了。


    2、代码演示样例

    2.1 button的代码演示样例

    <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/menubar_home_pressed" />
        <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/menubar_home_pressed" />
        <item android:state_enabled="true" android:drawable="@drawable/menubar_home_normal" />
        <item android:state_focused="true" android:drawable="@drawable/menubar_home_pressed" />
    </selector>

    上面的这段代码,表示的是一个button的动画效果。

    2.2 选中与失去焦点

    </pre><pre name="code" class="html"><pre name="code" class="html"><selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/menubar_home_pressed" android:state_selected="true"/>
        <item android:drawable="@drawable/menubar_home_normal"/>     
    </selector>
    
    
    上面的代码中,item部分第一行表示选中状态。第二行表示默认状态。


    3、问题解析

            在2.2中的代码。我们假设写成下面形式:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/menubar_home_normal"/>    
        <item android:drawable="@drawable/menubar_home_pressed" android:state_selected="true"/> 
    </selector>

             假设2.2中的item位置交换一下。第一行放在第二行的以下。那么我们能够在xml文件里看到一条黄色的波浪线。例如以下图所看到的:

            我们能够看到。代码中有这种提示:

    This item is unreachable because a previous item (item #1) is a more general match than this one

           这句话就是说第二行item之前。不能有元素出现。假设这样放置,会出现button一直是默认状态,而无法获得焦点的情况。


  • 相关阅读:
    Windows python 鼠标键盘监控及控制
    python 执行adb shell 命令
    python Windows提示框
    判断function属于函数或方法
    生成不同时间下的LOG
    pyqt5 QCalendar类中常用的方法
    python字符串大小写转换
    configparser 模块的基本方法
    QGridLayout类中常用的方法
    Day049--jQuery的文档操作和事件介绍
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6791788.html
Copyright © 2020-2023  润新知