Android. How do I keep a button displayed as PRESS

2019-01-12 10:19发布

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

6条回答
Explosion°爆炸
2楼-- · 2019-01-12 10:33

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>
查看更多
Luminary・发光体
3楼-- · 2019-01-12 10:42

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.

查看更多
干净又极端
4楼-- · 2019-01-12 10:49

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);
        }
    });
查看更多
放我归山
5楼-- · 2019-01-12 10:49

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.

查看更多
Rolldiameter
6楼-- · 2019-01-12 10:54

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:

查看更多
叼着烟拽天下
7楼-- · 2019-01-12 10:59

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.

查看更多
登录 后发表回答