繁体   English   中英

按下和释放时如何更改 ImageButton 的图像?

[英]how to change the ImageButton's image when it is pressed and released?

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

创建一个选择器(它是一个 xml 文件)放在可绘制文件夹中。 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>

为此使用选择器...这里是此链接.. http://developer.android.com/reference/android/widget/ImageButton.html

您可以使用相同的图像视图:

<ImageView
            android:id="@+id/iv1"
            android:src="@drawable/ic_new_delete0"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:visibility="visible" />

后面的代码:

ImageView _iv1 = _activity.FindViewById<ImageView>(Resource.Id.iv1);

        _iv1.Touch += (object sender, View.TouchEventArgs e) => {
            if (e.Event.Action == MotionEventActions.Down)
            {
                _iv1.SetImageResource(Resource.Drawable.ic_new_delete);
                //Do the task when button is pressed
            }
            else if (e.Event.Action == MotionEventActions.Up)
            {
               _iv1.SetImageResource(Resource.Drawable.ic_new_delete0);
               //do the task when button is released
            }
        };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM