C# 将TextChanged和KeyPressed事件处理程序添加到EditText使OnBackPressed不会命中

C# 将TextChanged和KeyPressed事件处理程序添加到EditText使OnBackPressed不会命中,c#,android,xamarin.android,C#,Android,Xamarin.android,我一直在开发一个有3个活动的应用程序(比如A、B、C) A使用StartActivity调用B(typeof(B)) B使用StartActivity(typeof(C))调用C,并使用 这个 C在线性布局中有一个全屏编辑文本。C也覆盖了 OnBackPressed调用this.finish()以完成自身并进行导航 回到A[因为A仍在后退]。在C的OnCreate方法中, EditText的事件处理程序已添加TextChanged事件并按了键 事件如下: public void OnCreate

我一直在开发一个有3个活动的应用程序(比如A、B、C)

  • A使用StartActivity调用B(typeof(B))
  • B使用StartActivity(typeof(C))调用C,并使用 这个
  • C在线性布局中有一个全屏编辑文本。C也覆盖了 OnBackPressed调用this.finish()以完成自身并进行导航 回到A[因为A仍在后退]。在C的OnCreate方法中, EditText的事件处理程序已添加TextChanged事件并按了键 事件如下:
  • public void OnCreate(Bundle SavedInstanceBundle)
    {
    base.OnCreate(SavedInstanceBundle);
    SetContentView(Resource.Layout.EditTextLayout);
    EditText EditText=findviewbyd(Resource.Id.EditText);
    //为EDITTEXT的TEXTCHANGED和KEYPRESSED事件添加事件处理程序
    editText.TextChanged+=editText\u TextChanged;
    editText.KeyPressed+=editText\u按键按下;
    }
    私有void editText\u TextChanged(对象发送方,textchangedventargs e)
    {
    }
    private void editText_键按下(对象发送者,View.KeyEventArgs e)
    {
    }
    public override void OnBackPressed()
    {
    base.OnBackPressed();
    这个;
    }
    
    这里的问题是,当eventhandler代码存在时,OnBackPressed 按下“硬件返回”时从不执行。但是,当添加事件 两个事件的处理程序代码都将被删除,最终OnBackPressed启动
    正常工作。

    您可以确定用户是否在按键事件处理程序中按下了后退键:

    private void editText_KeyPressed (object sender, View.KeyEventArgs e)
    {
        if (e.KeyCode == Keycode.Back)
        {
            this.Finish();
            e.Handled = true;
            return;
        }
    }
    

    谢谢这很有效。。。起初我以为这是xamarin的一个bug,但现在它起作用了。。。
    private void editText_KeyPressed (object sender, View.KeyEventArgs e)
    {
        if (e.KeyCode == Keycode.Back)
        {
            this.Finish();
            e.Handled = true;
            return;
        }
    }