0

我已经制作了一个 SurfaceView 来接受 OnTouchEvent,在这个视图中,我使用 Button.draw(Canvas) 手动绘制了一个 Button(而不是创建 ViewGroup)。

现在,这个按钮可以在我的 SurfaceView 上绘制,但是当我将一个 keyEvent 从 OnTouchEvent 传递给像 Button.onTouchEvent(keyEvent) 这样的按钮时,但是这个按钮无法显示点击效果

所以问题是我如何将 keyEvent 手动传递给 View 并显示它在布局中显示的内容????

有什么想法吗?谢谢!!

4

1 回答 1

0

我敢肯定有更好的方法来做到这一点......但我想不通。所以这就是我想出的。

我创建了自己的Button. 并MyButton在我的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>

您需要更改 to 的每个Button实例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();

    }

}

调用performVirtualClick函数……就可以了。

于 2010-09-27T04:34:39.153 回答