Android 按钮 setPressed 不改变动画
我想滥用标准按钮作为切换按钮,但仅限于长按时。因此,我首先将默认样式替换为按下、聚焦和默认状态的背景图像。
<?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 -->
<item android:state_focused="true" android:drawable="@drawable/btn_focused" /> <!-- focused -->
<item android:drawable="@drawable/btn_default" /> <!-- default -->
</selector>
我实现了 onClickListener 和 OnLongClickListener,如下所示:
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;
}
};
如果我执行长按,按钮不会将其背景更改为 state_pressed。如何才能让按钮保持按下状态?使用切换按钮不起作用,因为应该可以进行正常的单击操作。如果按下按钮的时间较长,按钮将被“锁定”。
非常感谢
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 -->
<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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以最后它起作用了。与使按钮状态故意为真相比,这是正确的方法
So Finally it works.It's right way as comparred to make the button state deliberatly true