【问题标题】:Change ImageView while button is pressed按下按钮时更改 ImageView
【发布时间】:2016-02-22 21:26:13
【问题描述】:

在 Android Studio 中,我知道可以使用选择器在不同状态下更改按钮的图像,例如

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/your_imagename_while_pressed" />
</selector>

我想知道ImageView 是否具有相同的功能,我有 4 个按钮,并且想在按下按钮时将我的 ImageView 更改为 4 个相应图像中的 1 个。当用户停止按下按钮时,我想将ImageView 改回其原始图像。

【问题讨论】:

    标签: java android xml imageview


    【解决方案1】:

    我相信你可以使用 onTouchListener

    b.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                    //Change image
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    //Restore image
                    return true;
                return false;
            }
        });
    

    【讨论】:

      【解决方案2】:

      您可以使用 ImageButton 来做到这一点。它是一个按钮,它有一个图像。 您也可以通过设置其中一个可绘制对象来使用常规按钮。但我认为 ImageButton 更好。

      See ImageButton details

      【讨论】:

      • 这行不通,因为我的按钮需要保持透明,我不想更改按钮图像,只更改背景图像。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多