简体   繁体   中英

How to set MultiLine and imeOptions=“actionDone” for an EditText on Android?

How to set these options at the same time :

  • android:minLines="3"
  • android:inputType="textMultiLine"
  • android:imeOptions="actionDone"

It seems as soon as I put android:inputType="textMultiLine" , the keyboard automatically replace the key OK by the key Enter . Does anyone know if it is possible to have both keys ?

NB : this answer is not what I am looking for. I would like both keys.

Hi i am also face the same issue finally i got solution for this.

change

android:inputType="textMultiLine"

to

android:inputType="text"

AND

Inside the .java file access EditText using id

editText.setHorizontallyScrolling(false);

editText.setMaxLines(3);

And Now implement the OnEditorAction over the editText.

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == 4) { //actionId 4 for actionDone And 6 for actionSend


                //perform action what you want

                return true;
            } else
                return false;
        }
    });

The only thing guaranteed is that Android will pass the inputType and imeOptions to the IME. What the IME does with them is implementation-dependent. Where some IMEs may decide there's sufficient screen real estate to display both keys when in multi-line mode, that behavior shouldn't be relied upon.

Have written an answer here for similar question : https://stackoverflow.com/a/42236407/7550472 and it turned out to be the saving grace for me as nothing else worked. For easier access, i'll paste the code here too for lazy people like me ;).

In your Java code :

////////////Code to Hide SoftKeyboard on Enter (DONE) Press///////////////
editText.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE);              //Set Return Carriage as "DONE"
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    {
                if (event == null) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        // Capture soft enters in a singleLine EditText that is the last EditText
                        // This one is useful for the new list case, when there are no existing ListItems
                        EditText.clearFocus();
                        InputMethodManager inputMethodManager = (InputMethodManager)  getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
                        inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
                    }

                    else if (actionId == EditorInfo.IME_ACTION_NEXT) {
                        // Capture soft enters in other singleLine EditTexts
                    } else if (actionId == EditorInfo.IME_ACTION_GO) {
                    } else {
                        // Let the system handle all other null KeyEvents
                        return false;
                    }
                } 
        else if (actionId == EditorInfo.IME_NULL) {
                    // Capture most soft enters in multi-line EditTexts and all hard enters;
                    // They supply a zero actionId and a valid keyEvent rather than
                    // a non-zero actionId and a null event like the previous cases.
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        // We capture the event when the key is first pressed.
                    } else {
                        // We consume the event when the key is released.
                        return true;
                    }
                } 
        else {
                    // We let the system handle it when the listener is triggered by something that
                    // wasn't an enter.
                    return false;
                }
                return true;
        }
});

The minLines defined in XML will remain the same while the other two attributes are not required as its handled in the Java code.

If you create a subclass of EditText and insert this function, it should solve your problem.

This question was answered at https://stackoverflow.com/a/5037488/7403656 however I made a little alteration which gets the imeOption from the xml instead of just setting it to the Done option.

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    InputConnection connection = super.onCreateInputConnection(outAttrs);
    int imeOptions = getImeOptions();
    int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION;
    if ((imeActions & imeOptions) != 0) {
        // clear the existing action
        outAttrs.imeOptions ^= imeActions;
        // set the DONE action
        outAttrs.imeOptions |= imeOptions;
    }
    if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
        outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
    }
    return connection;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM