选择器反应缓慢-有时在开始下一个活动之前根本不触发

问题描述

| 我定义了这个选择器:
<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">

    <!-- pressed -->
    <item android:state_pressed=\"true\"
          android:drawable=\"@drawable/backarrow_blueshiny\" />

    <!-- FOCUSED --> 
    <item android:state_focused=\"true\"
          android:drawable=\"@drawable/backarrow_blackshiny\" />

    <!-- DEFAULT -->
    <item android:drawable=\"@drawable/backarrow_blackshiny\" />

</selector>
并与此按钮一起使用:
    <RelativeLayout
    android:orientation=\"horizontal\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"wrap_content\" 
    android:background=\"@drawable/bottomborder_glossy\">

        <!-- BACK -->
        <ImageButton
        android:id=\"@+id/filter_button_back\"
        android:layout_width=\"90dip\" 
        android:layout_height=\"wrap_content\" 
        android:src=\"@drawable/selector_back_button\"
        android:background=\"#00000000\"
        android:layout_alignParentLeft=\"true\"
        android:layout_alignParentBottom=\"true\"/>

    </RelativeLayout>
并且onTouch事件包括
public boolean onTouch(View v,MotionEvent event)
{
    final int actionPerformed = event.getAction();
    final int widgetID = v.getId();

    if (actionPerformed == MotionEvent.ACTION_UP)
    {
        switch (widgetID)
        {
            case R.id.filter_button_back:
            {
                this.finish();
                break;
            }

        }

    }

    return false;
}
该按钮的作用是退出当前活动
this.finish()
但是,在我的测试中,按钮并非总是切换到“ backarrow_blueshiny”,即按下时非常快。 因此,问题在于选择器的启动速度比onTouch(MotionEvent.ACTION_UP)事件的启动速度慢。 我可以做些什么来确保选择器不“滞后”?     

解决方法

您是说比典型的Android选择器行为更慢吗?根据我的经验,在按下按钮和选择按钮之间总是存在一点延迟-我认为这是为了避免在您只想滚动时显示触摸事件,但是它会总是让我很烦。如果超出典型行为,请尝试仅使用将,4设置为选择器的按钮,而不是将ѭ5设置为选择器的ImageButton。 例如
<Button
    android:layout_width=\"90dip\"
    android:layout_height=\"wrap_content\"
    android:background=\"@drawable/selector_back_button\"
    android:layout_alignParentLeft=\"true\"
    android:layout_alignParentBottom=\"true\" 
    />
    ,尝试将视图设置为在OnTouchListener中以编程方式按下:
@Override
public boolean onTouch(View v,MotionEvent event) {
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            button.setPressed(true);
            break;
        case MotionEvent.ACTION_UP:
            // optional,should work without next line
            // button.setPressed(false); 
            break;
    }

    return false;
}
    ,我有同样的问题。在模拟器中,它甚至不会更改按钮的颜色。这是由“ onTouch”引起的。如果您使用onClickListener而不是onTouchListener,它将按预期进行反应。 如我所见,您没有使用任何多点触控功能,所以您不使用onClick并摆脱了
   if (actionPerformed == MotionEvent.ACTION_UP)