Android Use Done button on Keyboard to click button

后端 未结 13 1263
-上瘾入骨i
-上瘾入骨i 2020-12-02 06:30

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

相关标签:
13条回答
  • 2020-12-02 07:03

    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
           }
    };
    
    0 讨论(0)
  • 2020-12-02 07:06

    Kotlin Solution

    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
    }
    

    Kotlin Extension

    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
        }
    }
    

    Don't forget to add these options to your edittext

    <EditText ...
        android:imeOptions="actionDone"
        android:inputType="text"/>
    

    If you need inputType="textMultiLine" support, read this post

    0 讨论(0)
  • 2020-12-02 07:07

    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
      }
    }
    
    0 讨论(0)
  • 2020-12-02 07:08

    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;
            }
        });
    
    0 讨论(0)
  • 2020-12-02 07:10

    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;
    }
    
    0 讨论(0)
  • 2020-12-02 07:11

    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;
            }
        });
    
    0 讨论(0)
提交回复
热议问题