首页 > 解决方案 > ImageButton 在按下和释放时不会改变“背景”

问题描述

我正在制作一个有按钮的应用程序,当点击一个按钮时,它会播放声音。我的问题是我找不到在按下和释放时更改 mSoundButton 的背景的方法。我使用'background'而不是'src',所以我可以缩小按钮而不用从边缘切割。我还没有添加声音,因为我想在开始添加声音之前解决这个问题。

这是我在 MainActivity.java 中的按钮代码(它可以正常工作,但不是我想要的方式。按下但未释放时它会改变背景。)

        final ImageButton mSoundBtn;
        final boolean[] soundBtnClicked = {false};
        mSoundBtn = findViewById(R.id.soundButton);
        mSoundBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(soundBtnClicked[0])
                    mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
                else
                    mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);

                soundBtnClicked[0] = !soundBtnClicked[0];
            }
        });

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/soundButton"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@drawable/button_not_clicked"
        android:contentDescription="TODO" />

</LinearLayout>

更新(问题已解决):

我希望 mSoundBtn 在按下时更改图像,然后在释放时再次更改图像。我使用Handler类和postDelayed()方法在两个图像更改之间创建延迟。它实际上并没有等待用户释放按钮,但仍然解决了我的问题。以下是我找到的解决方案:

public ImageButton mSoundBtn;
Handler h = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSoundBtn = findViewById(R.id.soundButton);
    mSoundBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
            h.postDelayed(new Runnable() {
                public void run() {
                    mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);
                }
            }, 1000); // 1 Second
        }
    });

当 mSoundBtn 被点击时会发生什么,它会改变图像并等待 1000 毫秒(1 秒),然后变回之前的图像,当它被按下时会创建某种动画,以便用户可以理解何时按下按钮。

感谢任何试图提供帮助的人,感谢您的阅读。:)

标签: androidandroid-studio

解决方案


您不应将其定义为“最终”
示例:

    public class SampleActivity extends AppCompatActivity {

    //Variables
    public ImageButton mSoundBtn;
    public boolean isSoundBtnSelected = false; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        mSoundBtn = findViewById(R.id.soundButton);
        mSoundBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(!isSoundBtnSelected)
                    mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
                else
                    mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);

                isSoundBtnSelected = !isSoundBtnSelected;
            }
        });
      }

    }

推荐阅读