Android选择器的按下状态无法正常工作

5

我制作了控制电动升降桌的安卓和Arduino应用程序。我将这个项目上传到Github(https://github.com/neosarchizo/MyStandingDesk)。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/down_button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/down_button" />
</selector>

我想在MainActivity中使用选择器来为按钮设置样式。如果我按下某个按钮,那么该按钮的图像必须更改。但是它没有起作用。所以我通过检查MotionEvent来编程更改按钮的图像。

btnDown.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        btnDown.setImageResource(R.drawable.down_button_pressed);
                        sendCommand("a");
                        break;
                    case MotionEvent.ACTION_UP:
                        btnDown.setImageResource(R.drawable.down_button);
                        sendCommand("s");
                        break;
                }
                return true;
            }
        });

同时,我还在布局XML文件中为选择器编写了以下代码。

<ImageButton
    android:id="@+id/btnDown"
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_margin="30dip"
    android:layout_weight="1"
    android:scaleType="fitCenter"
    android:background="@android:color/transparent"
    android:src="@drawable/selector_down_button" />
2个回答

6

改变这个

android:src="@drawable/selector_down_button"

to

android:background="@drawable/selector_down_button"

当你接收到 ACTION_DOWN 事件时,可能希望启用 Pressed 状态,并在接收到 ACTION_UP 事件时禁用它。

  btnDown.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            btnDown.setImageResource(R.drawable.down_button_pressed);
                            sendCommand("a");
                            view.setPressed(true);
                            break;
                        case MotionEvent.ACTION_UP:
                            btnDown.setImageResource(R.drawable.down_button);
                            sendCommand("s");
                            view.setPressed(false);
                            break;
                    }
                    return true;
                }
            });

我已经尝试过了,但是它也没有起作用。请克隆我的项目并进行测试。 - neosarchizo
但我想使用按下事件。如果我使用点击事件,那么我如何知道是否触发了按下事件? - neosarchizo
有一个解决方案在我脑海中浮现,如果你能以那种方式做到,那是可能的。 - Jignesh Jain
我不是很清楚。以下步骤正确吗?1)添加LinearLayout 2)将按钮添加到布局中3)不要将选择器可绘制对象设置为布局和按钮4)那么我该如何更改图像?通过编程实现吗? - neosarchizo
  1. LinearLayout与Selector相同
  2. ImageButton在触摸时更改背景
  3. 取主LinearLayout
  4. 将ImageButton放入LinearLayout中
  5. 现在在Action_Down上设置LinearLayout和Button的背景
  6. 现在对于ActionUp做同样的操作
- Jignesh Jain
显示剩余8条评论

2

只需尝试这个

 android:background="@drawable/selector_down_button"

将选择器设置为background


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接