Android 单击后,用按钮中的另一个图像替换图像
我用了一个按钮作为菜单。我在这个按钮中使用了向下箭头和向上箭头图像。默认情况下,当我单击按钮时会显示向下箭头图像,我希望此向下箭头图像更改为向上箭头,并且应保持启用状态 这是我在xml文件中的按钮声明Android 单击后,用按钮中的另一个图像替换图像,android,button,Android,Button,我用了一个按钮作为菜单。我在这个按钮中使用了向下箭头和向上箭头图像。默认情况下,当我单击按钮时会显示向下箭头图像,我希望此向下箭头图像更改为向上箭头,并且应保持启用状态 这是我在xml文件中的按钮声明 <Button android:id="@+id/patientUtilityButton" android:layout_width="300dp" android:layout_height="50dp" andro
<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"/>
这是我的绘图文件,用于在单击按钮后替换图像
它正在替换图像,但按下相同的向下箭头后,图像显示出来。请有人帮助。非常简单。
您可以使用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>
我在应用程序中使用此复选框。。我为此设置了可绘制属性,并使用android:button=“@null”。现在它看起来像按钮。:)
<?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>