4

如何同时设置这些选项:

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

好像我一放android:inputType="textMultiLine",键盘就自动把OK键换成了Enter 键。有谁知道是否可以同时拥有两个密钥?

注意:这个答案不是我想要的。我想要两把钥匙。

4

4 回答 4

3

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

改变

android:inputType="textMultiLine"

android:inputType="text"

在 .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;
        }
    });
于 2016-09-29T12:56:42.067 回答
0

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

于 2012-08-17T12:23:04.977 回答
0

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

这个问题在https://stackoverflow.com/a/5037488/7403656得到了回答,但是我做了一些改动,从 xml 中获取了 imeOption,而不是仅仅将其设置为 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;
}
于 2017-04-27T13:12:39.100 回答
0

在这里为类似问题写了一个答案:https ://stackoverflow.com/a/42236407/7550472事实证明,这对我来说是一种可取之处,因为没有其他任何方法。为了更容易访问,我也将代码粘贴到这里给像我这样的懒人;)。

在您的 Java 代码中:

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

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

于 2017-02-14T21:24:46.497 回答