Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?

折月煮酒 提交于 2019-11-27 16:15:49

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.

Gunnar Bernstein

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);
        }
    });

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.

moin

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:

user527405

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.

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!