views:

40

answers:

1

Hello,i have made a surfaceView to accept a OnTouchEvent,and in this view, i draw a Button manually with Button.draw(Canvas)(not make a ViewGroup).

now, this button can be drawn on my SurfaceView, but when i pass a keyEvent from OnTouchEvent to Button like Button.onTouchEvent(keyEvent), but this button cannot show the click effect

so the question is how can i pass the keyEvent to a View manully and it show what it shown in a Layout????

Any ideas?thx!!

A: 

I'm sure there's a better way to do this...but i couldn't figure it out. So here's what i came up with.

I created my own extension of Button. And create components of MyButton inside my xmls.

<com.stackoverflow.android.example.MyButton android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></com.stackoverflow.android.example.MyButton>

You'll need to change every instance of Button to com.stackoverflow.android.example.MyButton.

package com.stackoverflow.android.example;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.Button;

public class MyButton extends Button {

    final int MSG_MYBUTTON_HIGHLIGHT_ON = 0;
    final int MSG_MYBUTTON_HIGHLIGHT_OFF = 1;

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    Handler mHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case MSG_MYBUTTON_HIGHLIGHT_ON:
                setPressed(true);
                sendEmptyMessageDelayed(MSG_MYBUTTON_HIGHLIGHT_OFF, ViewConfiguration.getPressedStateDuration());
                break;
            case MSG_MYBUTTON_HIGHLIGHT_OFF:
                setPressed(false);
            default:
                break;
            }
        };
    };

    public void performVirtualClick()
    {
        mHandler.sendEmptyMessage(MSG_MYBUTTON_HIGHLIGHT_ON);
        performClick();

    }

}

Call the performVirtualClick function...that'll do.

st0le