1

Originally I had my button connected to a method through the XML like this:

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_button_green"
    android:onClick="start_process"
    android:text="@string/button_send"
    android:textSize="50sp" />

Then I decided to add an onTouchListener so that I could handle ACTION_UP events:

    private OnTouchListener myListener = new OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //start recording
            System.out.println("DOWN");
            start_process();

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            System.out.println("UP");   
        }
        return true;
    }
};

The listener works great, but I've noticed that the states for my button have disappeared. The button had different colors for if it had been pressed, but this no longer happens now that the button is interacting through the listener. Here is what is defined in @drawable/custom_button_green:

<?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_default_pressed" />
<item android:state_focused="false" android:state_enabled="true"
    android:drawable="@drawable/btn_default_normal_green" />
<item android:state_focused="false" android:state_enabled="false"
    android:drawable="@drawable/btn_default_normal_disable" />
<item android:state_focused="true" android:state_enabled="true"
    android:drawable="@drawable/btn_default_selected" />
<item android:state_enabled="true"
    android:drawable="@drawable/btn_default_normal_green" />
<item android:state_focused="true"
    android:drawable="@drawable/btn_default_normal_disable_focused" />
<item
     android:drawable="@drawable/btn_default_normal_disable" />

Can anyone help me figure out why the button no longer follows these states?

Thanks!

4

1 回答 1

1

我认为onTouch您的侦听器的方法应该 return false,否则您只需覆盖在触摸事件上更新可绘制对象的“正常”行为。

于 2013-04-03T14:48:09.167 回答