setOnEditorActionListener()方法不使用inputtype textMultiline以及键盘中的enter按钮

时间:2018-04-23 10:50:36

标签: java android

当我在xml中使用inputtype作为textmultiline而在edittext中使用imeiOptions作为KEYCODE_ENTER时,我遇到了一个问题。然后我的setOnEditorActionListener无效,但如果我将inputtype更改为text,那么它的工作正常。我无法理解为什么这不适用于textMultiline。     我在xml中定义的Edittext是 - :

  <EditText
            android:id="@+id/etremarks"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_200sdp"
            android:background="@drawable/shape_edittext_pink_border"
            android:fontFamily="@font/share"
            android:gravity="top"
            android:hint="Enter Message"
            android:inputType="textMultiLine"
            android:padding="5dp"
            android:textColor="#000000" />


and my code for edittext in java file is-:


 etremarks.addTextChangedListener(mTextEditorWatcher);
        etremarks.setImeOptions(KeyEvent.KEYCODE_ENTER);

        etremarks.setOnEditorActionListener(new TextView.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event)
            {
                boolean handled = false;
                if (actionId == KeyEvent.KEYCODE_ENTER)
                {
                    // Handle pressing "Enter" key here
                    Toast.makeText(SendBulkSmsToClients.this, etremarks.getText(), Toast.LENGTH_SHORT).show();

                    handled = true;
                }
                return handled;
            }
        });

请分享您对此问题的宝贵意见

感谢

2 个答案:

答案 0 :(得分:0)

添加此行android:imeOptions="actionSend"并在代码中处理如下所示

 @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }

答案 1 :(得分:0)

使用

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);

和XML:

android:inputType="textMultiLine"

来源:Multi-line EditText with Done action button