本文介绍了按下并释放ImageButton时如何更改图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的android应用程序中,ImageButton在按下并释放时更改其图像,而在再次按下释放时,ImageButton的图像将被改回,该怎么做?

I want that in my android application, the ImageButton change its image when it is pressed and released, and when it is pressed released again, the image for ImageButton will be changed back , how to do that?

推荐答案

创建一个放置在drawable文件夹中的选择器(它是一个xml文件).并在xml中将实际图像android:background="@drawable/imageselector"插入该xml的五个路径,或者在程序中也可以使用imageview.setBackgroundDrawable(R.drawable.imageselector)

create a selector (it is an xml file) put in drawable folder. and in xml five path of that xml instaed of actual image android:background="@drawable/imageselector" or in program also you can get the same using imageview.setBackgroundDrawable(R.drawable.imageselector)

以下是我的选择器 imageselector.xml

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

这篇关于按下并释放ImageButton时如何更改图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 09:01