开发者

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

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 开发者_Go百科the entire action then reset to the button_normal image?

Thank you for your time


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.


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.


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:


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>


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜