" preSS和定妆QUOT;在Android按钮需要改变状态(自定义XML选择器)使用onTouchListener [英] "Press and hold" button on Android needs to change states (custom XML selector) using onTouchListener

查看:168
本文介绍了" preSS和定妆QUOT;在Android按钮需要改变状态(自定义XML选择器)使用onTouchListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪些需要有preSS并持有的功能,因此,而不是使用onClickListener,我使用onTouchListener,使应用程序可以反应

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,

 MotionEvent.ACTION_UP

根据如何快速地那两个事件被触发,我可以在这两者之间的时间运行pressAndHoldHandler

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

不管怎样,长话短说:我有相同的应用程序,不需要preSS和保持功能众多的基本的按钮,所以他们使用的是onClickListener

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.

这些按钮的每一个人都被定制图形用自己的XML文件中选择:

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

<?xml version="1.0" encoding="UTF-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_enabled="false"
        android:drawable="@drawable/btn_chicken_off" />

    <item
        android:state_enabled="true"
        android:state_pressed="true"
        android:drawable="@drawable/btn_chicken_s3" />

    <item
        android:state_enabled="true"
        android:state_focused="true"
        android:drawable="@drawable/btn_chicken_s2" />

    <item
        android:state_enabled="true"
        android:drawable="@drawable/btn_chicken_off" />

</selector>

所以,现在的问题是:上述选择没有得到与onTouchListener访问。只有onClickListener将拉动与它自己的方法的onclick()部分中的状态变化,因此这些preSS并持有的按钮永远不会改变的状态。 pretty的可怕反馈给用户。

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.

我目前正在迫使上述ACTION_DOWN的开关外壳和ACTION_UP内通过执行以下操作:

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

不过,这似乎是一个黑客,它缺少了集中而不是pressed的阶段。

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

有人绊倒过吗?

推荐答案

使用了 view.set pressed()函数来模拟pressed行为由你自己。

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

您可能会想启用的 pressed 的状态,当你得到 ACTION_DOWN 事件,并禁用它,当你得到的 ACTION_UP 事件。

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.

此外,这将是其禁用的情况下,用户滑出按钮的一个好主意。凤云 ACTION_OUTSIDE 事件,如下面的例子:

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

这篇关于&QUOT; preSS和定妆QUOT;在Android按钮需要改变状态(自定义XML选择器)使用onTouchListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆