2013-04-18 118 views
10

幾周前我開始與Xamarin Studio合作,並且找不到解決下一個問題的方法: 創建了一個包含序列號的edittext。在按下輸入後,我想讓ro運行一個功能。 它工作正常,當我按輸入,函數運行沒有失敗,但我不能修改edittext的內容(我不能鍵入它)。Xamarin Android EditText輸入密鑰

代碼:

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與它的參數,這個問題保留。我可以修改內容但不能處理輸入鍵。

謝謝!

回答

5

當按下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; 
} 
9

最好的辦法是使用被設計爲觸發對輸入按鍵的EditorAction事件。這將是這樣的代碼:

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

,並能夠改變的文本輸入按鈕使用imeOptions你的XML:

<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" /> 
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; 
     } 
    }; 

0

更好的是創建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; 
      } 
     }; 
    } 
}