【问题标题】:changing image of imagebutton till it is pressed改变图像按钮的图像直到它被按下
【发布时间】:2011-09-16 20:51:30
【问题描述】:

我希望在我的android应用程序中,ImageButton在按下和释放时改变它的图像,当它再次按下释放时,ImageButton的图像将变回,怎么做? 我已经用选择器试过了。 但它不起作用。 请任何人帮助我。 谢谢

【问题讨论】:

    标签: android imagebutton


    【解决方案1】:

    在可绘制文件夹 a.xml 中创建文件 ::

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item
        android:state_pressed="true"
        android:drawable="@drawable/oneImage" />
      <item
        android:state_pressed="false"
        android:drawable="@drawable/secondImage" />
    </selector>
    

    并在您的主文件 :: 按钮中实现以下代码 ::

    <ImageButton android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="anything"
            android:id="@+id/first"
            android:background="@drawable/a"
            android:textColor="#FFFFFF" 
            android:textStyle="bold"
            ></ImageButton>
    

    【讨论】:

    • 这段代码不是说,当你按下时图像变为oneImage,而当没有按下时,图像是secondImage? ,我认为问题是不同的。
    • 当你按下它时肯定会改变图像
    • 解析 selector.xml 文件失败给我异常
    【解决方案2】:

    使用以下选择器。

    <?xml version="1.0" encoding="utf-8"?>
    <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
       <item 
        android:state_pressed="true"
        android:drawable="@drawable/pressedImage"/>
      <item 
        android:state_focused="true"
        android:drawable="@drawable/normalImage"/>
      <item android:drawable="@drawable/normalImage"/>
    </selector>
    

    【讨论】:

    • 检查我编辑的代码,可能有一些程序错误,但你需要它的逻辑。
    • 使用此代码,图像即使在发布后也保持不变。我希望当图像被按下时,图像被按下的那一刻发生变化。当它被释放时,它的资源再次设置为原始图像......
    • 那么请更改您的问题,当我阅读时它给出了不同的含义。
    猜你喜欢
    • 2011-12-17
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    相关资源
    最近更新 更多