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

发布于 2024-11-17 14:42:08 字数 116 浏览 4 评论 0原文

我希望在我的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北风几吹夏 2024-11-24 14:42:08

创建一个选择器(它是一个 xml 文件)放入可绘制文件夹中。在 xml 中,该 xml 的五个路径代替了实际图像 android:background="@drawable/imageselector" 或在程序中,您也可以使用 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>

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)

Following is my selector
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>
浅笑轻吟梦一曲 2024-11-24 14:42:08

您可以使用图像视图进行相同的操作:

<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
            }
        };

You can use Image View for the same:

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

Code behind:

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
            }
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文