android xml ontouch,“Press and hold” button on Android needs to change states (custom XML selector) ...

问题

I've got a button graphic which needs to have "press and hold" functionality, so instead of using onClickListener, I'm using onTouchListener so that the app can react to

MotionEvent.ACTION_DOWN,

and

MotionEvent.ACTION_UP

Depending on how quickly those two events are triggered, I can run a "pressAndHoldHandler" in the time between the two.

Anyway, long story short: I have numerous "basic" buttons in the same app that don't require the press and hold functionality, so they are using the onClickListener.

Every single one of these buttons have been customized graphically with their own XML selector file:

xmlns:android="http://schemas.android.com/apk/res/android">

android:state_enabled="false"

android:drawable="@drawable/btn_chicken_off" />

android:state_enabled="true"

android:state_pressed="true"

android:drawable="@drawable/btn_chicken_s3" />

android:state_enabled="true"

android:state_focused="true"

android:drawable="@drawable/btn_chicken_s2" />

android:state_enabled="true"

android:drawable="@drawable/btn_chicken_off" />

So, the problem here is: The above selector doesn't get accessed with the onTouchListener. Only the onClickListener will pull in the state changes with the onClick() section of its own method, so these "press and hold" buttons never change state. Pretty terrible feedback for the user.

I'm currently forcing the above inside the switch case of ACTION_DOWN and ACTION_UP by doing the following:

if (action == MotionEvent.ACTION_DOWN) {

btn_chicken.setBackgroundResource(R.drawable.btn_chicken_s3);

}

else

if (action == MotionEvent.ACTION_UP) {

btn_chicken.setBackgroundResource(R.drawable.btn_chicken_off);

}

But it seems like a hack, and it's missing the "focused but not pressed" stage.

Anybody tripped across this before?

回答1:

Use the view.setPressed() function to simulate the pressed behavior by yourself.

You would probably want to enable the Pressed state when you get ACTION_DOWN event and disable it when you get the ACTION_UP event.

Also, it will be a good idea to disable it in case the user slide out of the button. Catching the ACTION_OUTSIDE event, as shown in the example below:

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:

v.setPressed(true);

// Start action ...

break;

case MotionEvent.ACTION_UP:

case MotionEvent.ACTION_OUTSIDE:

case MotionEvent.ACTION_CANCEL:

v.setPressed(false);

// Stop action ...

break;

case MotionEvent.ACTION_POINTER_DOWN:

break;

case MotionEvent.ACTION_POINTER_UP:

break;

case MotionEvent.ACTION_MOVE:

break;

}

return true;

}

回答2:

Make sure to return false at the end of the onTouchListener() function. :)

回答3:

You can just set the button onClickListener and leave its onClick method empty.

Your logic implement inside onTouch.

That way you'll have the press effect.

P.S You don't need all those state in the selector you can simply use:

回答4:

I figured it out! Just use view.setSelected() as follows:

@Override

public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == MotionEvent.ACTION_DOWN){

yourView.setSelected(true);

...

return true;

}

else if(event.getAction() == MotionEvent.ACTION_UP){

yourView.setSelected(false);

...

return true;

}

else

return false;

}

That'll make your selector work even if your view has an onTouch listener :)

回答5:

Better solution:

@Override

public void onClick(View v) {

if (v.isActivated())

v.setActivated(false);

else

v.setActivated(true);

}

来源:https://stackoverflow.com/questions/12590169/press-and-hold-button-on-android-needs-to-change-states-custom-xml-selector

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值