【问题标题】:Xamarin Android EditText enter keyXamarin Android EditText 输入键
【发布时间】:2013-04-11 23:48:57
【问题描述】:

我几周前开始使用 Xamarin Studio,但找不到下一个问题的解决方案: 创建了一个包含序列号的edittext。我想在按下 Enter 后运行一个函数。 它工作正常,当我按 Enter 时,函数运行没有失败,但我无法修改编辑文本的内容(我无法输入)。

代码:

EditText edittext_vonalkod = FindViewById<EditText>(Resource.Id.editText_vonalkod);
edittext_vonalkod.KeyPress += (object sender, View.KeyEventArgs e) =>
{
    if ((e.Event.Action == KeyEventActions.Down) && (e.KeyCode == Keycode.Enter))
    {
        //Here is the function
    }
};

这是控件的代码:

<EditText
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:layout_below="@+id/editText_dolgozo_neve"
    p1:id="@+id/editText_vonalkod"
    p1:layout_alignLeft="@+id/editText_dolgozo_neve"
    p1:hint="Vonalkód"
    p1:text="1032080293"
    p1:layout_toLeftOf="@+id/editText_allapot" />

我尝试将edittext_vonalkod.TextCanged 与它的参数一起使用,问题保留。我可以修改内容但不能处理 Enter 键。

谢谢!

【问题讨论】:

    标签: android key keypress xamarin enter


    【解决方案1】:

    最好的方法是使用EditorAction 事件,该事件旨在在Enter 键按下时触发。应该是这样的代码:

    edittext_vonalkod.EditorAction += (sender, e) => {
        if (e.ActionId == ImeAction.Done) 
        {
            btnLogin.PerformClick();
        }
        else
        {
            e.Handled = false;
        }
    };
    

    为了能够更改 Enter 按钮的文本,请在您的 XML 中使用 imeOptions

    <EditText
        p1:layout_width="wrap_content"
        p1:layout_height="wrap_content"
        p1:layout_below="@+id/editText_dolgozo_neve"
        p1:id="@+id/editText_vonalkod"
        p1:layout_alignLeft="@+id/editText_dolgozo_neve"
        p1:hint="Vonalkód"
        p1:text="1032080293"
        p1:layout_toLeftOf="@+id/editText_allapot" 
        p1:imeOptions="actionSend" />
    

    【讨论】:

    • 之后如何隐藏键盘?
    【解决方案2】:

    当按下的键为 ENTER 时,您需要将事件标记为未处理。将以下代码放入您的 KeyPress 处理程序中。

    if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter) 
    {
       // Code executed when the enter key is pressed down
    } 
    else 
    {
       e.Handled = false;
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      
      
          editText = FindViewById(Resource.Id.editText);    
          editText.KeyPress += (object sender, View.KeyEventArgs e) => 
          {
              e.Handled = false;
              if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
              {
                  //your logic here
                  e.Handled = true;
              }
          };
      
      

      【讨论】:

        【解决方案4】:

        更好的是为EditText创建可重用的扩展(EditTextExtensions.cs):

        public static class EditTextExtensions
        {
            public static void SetKeyboardDoneActionToButton(this EditText editText, Button button)
            {
                editText.EditorAction += (sender, e) => {
                    if (e.ActionId == ImeAction.Done)
                    {
                        button.PerformClick();
                    }
                    else
                    {
                        e.Handled = false;
                    }
                };
            }
        }
        

        【讨论】:

          【解决方案5】:
           editText.KeyPress += (object sender, View.KeyEventArgs e) =>
                      {
                              if ((e.KeyCode == Keycode.Enter))
                              {
                                 // `enter code here`
                              }
                      };
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-12
            • 2014-05-28
            • 1970-01-01
            • 2012-09-12
            • 1970-01-01
            • 1970-01-01
            • 2012-10-30
            • 1970-01-01
            相关资源
            最近更新 更多