问题
I am having some very strange problems with the EditText control in Mono for Android. My solution is targeting 2.3 and I am debugging on a T Mobile VivaCity. Here is my AXML for the EditText
<EditText
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ctl_searchText" />
When I show the View containing the EditText the keyboard automatically appears, this is not a problem. The problem is I can't enter any numbers by tapping the numbers on the keyboard, the only way I can get a number to show in the text field is if I hold the key down and chose the number from a context menu. Although once I've entered a number in using this method I'm then not able to delete it. I've tried all sorts of input methods and had a look for similar issues in SO to no avail. Does this sound like an issue with the device? Or is there something glaringly obvious I'm not doing in the code/AXML?
== EDIT ==
I think I've narrowed the problem down, it has something to do with the KeyPress event handler used on the EditText. As the EditText represents a search field I have added the attribute android:singleLine="true" to stop the return key from adding an extra line and instead say "Done". When I add a KeyPress event handler to the control this is when it stops me from entering numbers, but without the handler it begins to function normally again. Here is what I have:
<EditText
android:id="@+id/ctl_searchText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
EditText ctl_searchText = FindViewById<EditText>(Resource.Id.ctl_searchText);
ctl_searchText.KeyPress += (object sender, View.KeyEventArgs e) =>
{
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
Toast.MakeText (this, ctl_searchText.Text, ToastLength.Short).Show ();
e.Handled = true;
}
};
With this code I cannot enter numbers into the text field, but I can enter letters. When I remove the event handler it works again, allowing me to enter all characters. I'm going to carry on investigating, this is very strange.
回答1:
Please cheack that you doesn't use OnKeyListener. If yes just check that onKey (View v, int keyCode, KeyEvent event) method return True if the listener has consumed the event, false otherwise. In your case it something like this:
ctl_searchText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_ENTER){
//do smth
return true;
}
return fasle;
}
});
回答2:
Try adding an else stating that e.Handled = false;
Your code will then look like:
ctl_searchText.KeyPress += (object sender, View.KeyEventArgs e) =>
{
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
Toast.MakeText (this, ctl_searchText.Text, ToastLength.Short).Show ();
e.Handled = true;
}
else
{
e.Handled = false;
}
};
来源:https://stackoverflow.com/questions/16281650/monodroid-edittext-input-method-wont-accept-numbers