如何设置多行和imeOptions=";“行动完成”;Android上的编辑文本?

如何设置多行和imeOptions=";“行动完成”;Android上的编辑文本?,android,android-softkeyboard,Android,Android Softkeyboard,如何同时设置这些选项: android:minLines=“3” android:inputType=“textMultiLine” android:imeOptions=“actionDone” 似乎只要我按下android:inputType=“textMultiLine”,键盘就会自动将OK键替换为Enter键。有人知道两把钥匙是否都可以用吗 注:这不是我要找的。我想要两把钥匙。唯一可以保证的是Android会将inputType和imeOptions传递给IME。IME如何处理它们取

如何同时设置这些选项:

  • android:minLines=“3”
  • android:inputType=“textMultiLine”
  • android:imeOptions=“actionDone”
似乎只要我按下android:inputType=“textMultiLine”,键盘就会自动将OK键替换为Enter键。有人知道两把钥匙是否都可以用吗


注:这不是我要找的。我想要两把钥匙。

唯一可以保证的是Android会将
inputType
imeOptions
传递给IME。IME如何处理它们取决于实现。当一些IME可能决定在多行模式下有足够的屏幕空间来显示两个键时,这种行为不应该被依赖。

嗨,我也面临同样的问题,最后我找到了解决方案

改变

android:inputType="textMultiLine"

在.java文件中,使用id访问EditText

editText.setHorizontallyScrolling(false);

editText.setMaxLines(3);
现在在editText上实现OnEditorAction

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

我在这里为类似的问题写了一个答案:结果证明,这是对我的救赎恩典,因为其他任何东西都不起作用。为了便于访问,我也将代码粘贴到这里,以供像我这样的懒人使用;)

在Java代码中:


XML中定义的
minLines
将保持不变,而其他两个属性不需要在Java代码中处理。

如果您创建EditText的子类并插入此函数,它应该可以解决您的问题

这个问题在上得到了回答,但是我做了一点修改,从xml中获取IME选项,而不是仅仅将其设置为Done选项

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

对我来说,我可以编译自己的输入法,但选项没有传递,这是安卓决定放在那里的。
////////////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;
        }
});
@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;
}