Ok in my app I have a field for the user to input a number. I have the field set to only accept numbers. When the user clicks on the field it brings up the keyboard. On the
Try this for Xamarin.Android (Cross Platform)
edittext.EditorAction += (object sender, TextView.EditorActionEventArgs e) {
if (e.ActionId.Equals (global::Android.Views.InputMethods.ImeAction.Done)) {
//TODO Something
}
};
The base way to handle the done action in Kotlin is:
edittext.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Call your code here
true
}
false
}
Use this to call edittext.onDone {/*action*/}
in your main code. Keeps it more readable and maintainable
edittext.onDone { submitForm() }
fun EditText.onDone(callback: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
callback.invoke()
true
}
false
}
}
<EditText ...
android:imeOptions="actionDone"
android:inputType="text"/>
If you need
inputType="textMultiLine"
support, read this post
Kotlin and Numeric keyboard
If you are using the numeric keyboard you have to dismiss the keyboard, it will be like:
editText.setOnEditorActionListener { v, actionId, event ->
if (action == EditorInfo.IME_ACTION_DONE || action == EditorInfo.IME_ACTION_NEXT || action == EditorInfo.IME_ACTION_UNSPECIFIED) {
//hide the keyboard
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
//Take action
editValue.clearFocus()
return true
} else {
return false
}
}
You can use this one also (sets a special listener to be called when an action is performed on the EditText), it works both for DONE and RETURN:
max.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Enter pressed");
}
return false;
}
});
You can implement on key listener:
public class ProbabilityActivity extends Activity implements OnClickListener, View.OnKeyListener {
In onCreate:
max.setOnKeyListener(this);
...
@Override
public boolean onKey(View v, int keyCode, KeyEvent event){
if(keyCode == event.KEYCODE_ENTER){
//call your button method here
}
return true;
}
I copied the following code from AndroidStudio when you create a LoginActivity. I use the ime attributes
In your layout
<EditText android:id="@+id/unidades" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="@string/prompt_unidades"
android:inputType="number" android:maxLines="1"
android:singleLine="true"
android:textAppearance="?android:textAppearanceSmall"
android:enabled="true" android:focusable="true"
android:gravity="right"
android:imeActionId="@+id/cantidad"
android:imeActionLabel="@string/add"
android:imeOptions="actionUnspecified"/>
In your Activity
editTextUnidades.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == R.id.cantidad || actionId == EditorInfo.IME_NULL) {
addDetalle(null);
return true;
}
return false;
}
});