单击后用按钮中的另一个图像替换图像

Replace image with another image in button after clicked

我已经将按钮用作菜单。我在此按钮内使用了向下箭头和向上箭头图像。默认情况下,当我单击按钮时会显示向下箭头图像我希望这个向下箭头图像变为向上箭头并且它应该保持启用状态。

这是我在 xml 文件中的按钮声明

    <Button
        android:id="@+id/patientUtilityButton"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:text="Patient Utilities"
        android:gravity="center"
        android:paddingRight="10dp"
        android:drawableRight="@drawable/patient_utility_selector"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:background="@drawable/round_corner"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/searchButton"
        android:layout_alignStart="@+id/searchButton"
        android:layout_below="@+id/searchButton"
        android:layout_marginTop="15dp"/>

这是我点击按钮后替换图像的可绘制文件

<item android:state_enabled="false"
    android:drawable="@drawable/arrow_down_float" />

<item android:state_pressed="true"
    android:state_enabled="true"
    android:drawable="@drawable/arrow_up_float" />

它正在替换图像,但在按下相同的向下箭头图像后 showing.Please 有人帮忙。

非常简单。 您可以使用 ImageButton 或 Button。

在您的 xml 文件中将背景设置为 imageButton,如下所示

 android:background="@drawable/button_down"

然后点击那个图片按钮就像

button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            button.setBackgroundResource(R.drawable.button_up);
        }
    });

希望对您有所帮助。

您应该使用切换按钮,而不是使用按钮。 下面列出了示例代码。

<ToggleButton 
        android:id="@+id/patientUtilityButton"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:background="@drawable/check"   //check.xml
        android:layout_margin="10dp"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_centerVertical="true"/>

在 drawable 中创建 check.xml 文件

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

    <item android:drawable="@drawable/down_image"
          android:state_checked="true" />

    <item android:drawable="@drawable/up_image"
        android:state_checked="false"/>

 </selector>