捕捉按钮上的上/下触摸

时间:2016-07-08 17:17:08

标签: android xamarin xamarin.android

我需要为特定按钮捕获OnKeyUp事件。所以我试图捕获所有OnKeyUp事件并过滤我想要的事件。这是我的代码;

[Android.Runtime.Register("onKeyUp", "(ILandroid/view/KeyEvent;)Z", "GetOnKeyUp_ILandroid_view_KeyEvent_Handler")]  

public Boolean OnKeyUp(int keyCode, KeyEvent bevent)
    {//do something
        return true;
    }

我在调试模式下运行并为return true设置一个断点;线。在按下应用程序中的按钮后,它永远不会进入该功能。怎么了?

1 个答案:

答案 0 :(得分:0)

对于Button向上/向下触摸实施IOnTouchListener

按钮示例:

[Activity(Label = "MPAndroidChartTest", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity, Android.Views.View.IOnTouchListener
{
    int count = 1;
    Button button;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        button = FindViewById<Button>(Resource.Id.myButton);

        button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
        button.SetOnTouchListener(this);
    }

    public bool OnTouch(View v, MotionEvent e)
    {
        if (button == null)
            button = FindViewById<Button>(Resource.Id.myButton);
        if (v == button)
        {
            switch (e.Action)
            {
                case MotionEventActions.Down:
                    Log.Debug("Ontouch", "Start Timer");
                    break;
                case MotionEventActions.Up:
                    Log.Debug("Ontouch", "Stop Timer");
                    break;
            }
        }
        return true;
    }
}

按键上/下按:

Activity覆盖OnKeyUp

输入示例:

public override bool OnKeyUp(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
    if (keyCode == Android.Views.Keycode.C)
        Log.Debug("KeyPress", "C Keycode");
    return base.OnKeyUp(keyCode, e);
}