安卓。如何使按钮保持显示为“已按下”,直到该按钮创建的操作完成为止?

发布于 2024-10-11 21:27:22 字数 332 浏览 4 评论 0原文

我有 button_focusedbutton_pressedbutton_normal 图像。当我按下按钮时,将显示 button_pressed 图像,并且与按下按钮相关的操作开始。

当我停止按下按钮时,操作会继续,但按钮会返回到显示的 button_normal 图像。

如何将整个操作期间显示的按钮图像设置为 button_pressed,然后重置为 button_normal 图像?

谢谢您的宝贵时间

I have button_focused, button_pressed, and button_normal images. When I press the button, the button_pressed image is displayed and the action related to the button pressing begins.

When I quit pressing the button, the action continues but the button returns to button_normal image being displayed.

How can I set the button image being displayed to button_pressed during the entire action then reset to the button_normal image?

Thank you for your time

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

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

发布评论

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

评论(7

孤独岁月 2024-10-18 21:27:22

我使用了一个类似的函数

void setHighlighted(boolean highlight) {
    button.setBackgroundResource( highlight
                                ? R.drawable.bbg_pressed
                                : R.drawable.button_background);
}

,其中button_background是定义在中的选择器
button_backgroung.xml

<?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/bbg_pressed"></item>
    <item android:state_focused="true" android:drawable="@drawable/bbg_selected"></item>
    <item android:drawable="@drawable/bbg_normal"></item>
</selector>

即这段代码不会干扰Android框架使用的按下状态;相反,它会更改背景,使按钮看起来被按下。

I used a function like

void setHighlighted(boolean highlight) {
    button.setBackgroundResource( highlight
                                ? R.drawable.bbg_pressed
                                : R.drawable.button_background);
}

where button_background is a selector defined in
button_backgroung.xml:

<?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/bbg_pressed"></item>
    <item android:state_focused="true" android:drawable="@drawable/bbg_selected"></item>
    <item android:drawable="@drawable/bbg_normal"></item>
</selector>

That is, this code does not interfere with the pressed state used by the Android framework; instead, it changes the background so that the button looks pressed.

长亭外,古道边 2024-10-18 21:27:22

如果您只需要两个状态按钮,则更清楚一点:

您不需要自己的 button.xml。您可以正常使用Android。

如果您单击按钮,button.setPressed(true) 将不起作用,因为一旦您松开按钮,Android 就会重置它。首先尝试设置另一个按钮的 setPressed 状态来查看效果。

这意味着,要在同一个按钮上使用它,必须将其设置为延迟。这是一个工作示例。当然,上面提到的改变背景的方法(通过长 id 18..)也有效。

   private final Handler mHandler = new Handler();
   rootView.findViewById(R.id.yourButton).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean pressed = false;
            if (v.getTag() instanceof Boolean)
                pressed = (boolean) v.getTag();
            final boolean newPressed = !pressed;
            // setTag to store state
            v.setTag(newPressed);
            final View vRun = v;
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    vRun.setPressed(newPressed);
                }
            };
            mHandler.post(run);
            // mHandler.postDelayed(run, 5);
        }
    });

To make it a bit clearer if you are just in need of a two state button:

You do not need your own button.xml. You can work with the normal of Android.

The button.setPressed(true) will not work if you are clicking the button, because Android will reset it once you let go of the button. Try to set another buttons setPressed state first to see the effect.

Which means, to use it on the same button it must be set delayed. Here is a working example. Of course, the mentioned approach (by long id 18..) on changing the background works too.

   private final Handler mHandler = new Handler();
   rootView.findViewById(R.id.yourButton).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean pressed = false;
            if (v.getTag() instanceof Boolean)
                pressed = (boolean) v.getTag();
            final boolean newPressed = !pressed;
            // setTag to store state
            v.setTag(newPressed);
            final View vRun = v;
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    vRun.setPressed(newPressed);
                }
            };
            mHandler.post(run);
            // mHandler.postDelayed(run, 5);
        }
    });
谷夏 2024-10-18 21:27:22

如果您在 onClick 方法中手动更改按钮中的图像,那么当操作完成时,它可以设置该按钮的正常图像。如果操作非常快,则更改将无法正确显示 - 它可能还需要延迟代码。

If you change the image in the button manually in its onClick method, then when an action finishes it can set the normal image for that button back. If the action is very quick then change will not show properly - it may need a delay code as well.

时光匆匆的小流年 2024-10-18 21:27:22

使用 (buttonName).setPressed(true)

并确保您已为可绘制对象保留了适当的 xml 文件,该文件定义了用于按下、聚焦等状态的可绘制对象:

Use (buttonName).setPressed(true)

And make sure you have kept the appropriate xml file for the drawable that defines which drawable to use for states like pressed,focused etc:

酒中人 2024-10-18 21:27:22

然后

NAME_OF_BUTTON.setImageResource(0xvalueofbutton_pressed image listed in R.java);

,当操作终止时,我复制了代码并插入了 button_normal 的整数值。

我之前这样做的方式不同,但我找不到我的代码的备份或硬拷贝。

再次感谢您的回复。

I used

NAME_OF_BUTTON.setImageResource(0xvalueofbutton_pressed image listed in R.java);

then when the action terminates I copied the code and inserted the integer value of button_normal.

I did this differently before and I cannot find my backups or hard copies of my code.

Thank you again for your responses.

雄赳赳气昂昂 2024-10-18 21:27:22

只是加上我的 2 美分作为另一种选择。您可以使用setSelected(true),而不是使用button.setPressed,一旦用户松开按钮,选择器就会丢失选择器的状态。只要确保完成操作后返回 setSelected(false) 即可。

        btn.setOnClickListener( view ->  {
        view.setSelected(true);
    });

并将可绘制选择器添加为按钮的背景:

<item android:drawable="@drawable/btn_selected" android:state_selected="true"></item>

Just adding my 2 cents as another alternative. Instead of button.setPressed, which will lose the state from the selector once the user let go of the button, you can use setSelected(true). Just make sure to go back with setSelected(false) when you finish the action.

        btn.setOnClickListener( view ->  {
        view.setSelected(true);
    });

And add the drawable selector as background for the button:

<item android:drawable="@drawable/btn_selected" android:state_selected="true"></item>
蘸点软妹酱 2024-10-18 21:27:22

老问题,但这是我所做的,看起来比上述所有问题都简单。在按钮的 onclick 中:

thisButton
    .getBackground()
    .setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);

我的按钮正在使用可绘制资源(不确定这是否重要),当用户单击一个资源时,我设置了我想要的颜色过滤器,该颜色过滤器与我用于“按下”状态的颜色相同,因此看起来像它保持按下状态。

要清除它,请使用:

thisButton.getBackground().clearColorFilter();

Old question but here's what I did that seems simpler than all of the above. In the button's onclick:

thisButton
    .getBackground()
    .setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);

My buttons are using a drawable resource (not sure if that matters) and when the user clicks one I set the color filter I want which is the same color I use for the "pressed" state so it looks like it stays pressed.

To clear it use:

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