Android 按钮 setPressed 不改变动画

发布于 2024-12-09 07:21:45 字数 1101 浏览 0 评论 0原文

我想滥用标准按钮作为切换按钮,但仅限于长按时。因此,我首先将默认样式替换为按下、聚焦和默认状态的背景图像。

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/btn_pressed" /> <!-- pressed -->
        <item android:state_focused="true" android:drawable="@drawable/btn_focused" /> <!-- focused -->
        <item android:drawable="@drawable/btn_default" /> <!-- default -->

    </selector>

我实现了 onClickListener 和 OnLongClickListener,如下所示:

private OnLongClickListener mFireHoldListener = new OnLongClickListener() {

    @Override
    public boolean onLongClick(View view) {
        Log.i(TAG, "Long FIRE");

        Button btn = (Button) view;
        btn.setPressed(true);
        btn.invalidate();
        Log.i(TAG, "isPressed: " + btn.isPressed());

        return false;
    }

};

如果我执行长按,按钮不会将其背景更改为 state_pressed。如何才能让按钮保持按下状态?使用切换按钮不起作用,因为应该可以进行正常的单击操作。如果按下按钮的时间较长,按钮将被“锁定”。

非常感谢

I would like to abuse a standard button as a toggle button, but only when its longpressed. Therefore I first replaced the default style with background images for pressed, focused and default state.

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/btn_pressed" /> <!-- pressed -->
        <item android:state_focused="true" android:drawable="@drawable/btn_focused" /> <!-- focused -->
        <item android:drawable="@drawable/btn_default" /> <!-- default -->

    </selector>

I implemented both, onClickListener and OnLongClickListener following:

private OnLongClickListener mFireHoldListener = new OnLongClickListener() {

    @Override
    public boolean onLongClick(View view) {
        Log.i(TAG, "Long FIRE");

        Button btn = (Button) view;
        btn.setPressed(true);
        btn.invalidate();
        Log.i(TAG, "isPressed: " + btn.isPressed());

        return false;
    }

};

If I perform a long click, the button doesn't change its background to the state_pressed. How can I keep the button pressed? Using a toggle button doesn't work as a normal click operation should be possible. If the button is pressed for a longer time, the button gets "locked".

Many Thanks

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

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

发布评论

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

评论(1

仙女山的月亮 2024-12-16 07:21:45

所以最后它起作用了。与使按钮状态故意为真相比,这是正确的方法

  @Override
    public boolean onLongClick(View view) {
        final Button btn = (Button) view;
        btn.post(new Runnable(
            public void run() {
                btn.setBackgroungResource(R.drawable.btn_pressed);
            }
        }

        return false;
    }

So Finally it works.It's right way as comparred to make the button state deliberatly true

  @Override
    public boolean onLongClick(View view) {
        final Button btn = (Button) view;
        btn.post(new Runnable(
            public void run() {
                btn.setBackgroungResource(R.drawable.btn_pressed);
            }
        }

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