开发者

Android Button setPressed does not change animation

开发者 https://www.devze.com 2023-04-12 02:25 出处:网络
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.

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开发者_JS百科 -->
        <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


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

精彩评论

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

关注公众号