13

我正在尝试设置一个侦听器,以了解EditText何时按下输入按钮。但它根本没有触发。我在装有 Android 4.2.2 的 LG Nexus 4 上对此进行了测试。setOnEditorActionListener可在装有 Android 2.3 的 Amazon Kindle Fire 上setImeActionLabel运行,但无法在任何地方运行!我也无法为按钮设置文本。Enter这是代码:

mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
            return true;
        }  
    });

我究竟做错了什么?我怎样才能解决这个问题?

4

9 回答 9

9

您可以使用TextWatcher

editText.addTextChangedListener(new TextWatcher() {
    
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
        
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
        
    @Override
    public void afterTextChanged(Editable s) {
        if (s.charAt(s.length() - 1) == '\n') {
              Log.d("TAG", "Enter was pressed");
        }
    }
});
于 2013-05-23T08:25:27.967 回答
8

确保您在布局文件中设置了 IME_ACTION:

<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

有关完整说明,请参阅http://developer.android.com/guide/topics/ui/controls/text.html

于 2014-10-07T05:01:05.933 回答
4

对我有用的是这个,我已将以下行添加到 EditText

android:imeOptions="actionSend"

此行使单击编辑文本时弹出的键盘具有发送按钮而不是搜索

在 setOnEditorActionListener 中,您覆盖以下方法以查找操作发送

@Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
        //implement stuff here
          }
        }
于 2017-12-19T22:19:35.780 回答
3

在 xml 文件中添加标签android:inputType="text" 到 EditText

于 2018-06-28T08:04:19.753 回答
2

我使用以下代码

<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/ic_magnify"
    android:layout_centerVertical="true"
    android:textSize="15sp"
    android:textColor="#000"
    android:id="@+id/input_search"
    android:inputType="text"
    android:background="@null"
    android:hint="Enter Address, City or Zip Code"
    android:imeOptions="actionSearch"/>

mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {

                if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE
                        || keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

                        geoLocate();

                }

                return false;
            }
        });
于 2018-11-02T13:06:37.087 回答
0

您可以尝试使用OnKeyListener

editText.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int actionId, KeyEvent event) {
            Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
            return false;
        }
    });
于 2013-05-23T07:44:11.577 回答
0

我明白了,它适用于 Activity 中的 Edittext,但不适用于 Fragment。

<EditText
                android:id="@+id/mEditText"
                android:imeOptions="actionSearch"
                android:imeActionLabel="搜索"
                android:inputType="text"
                android:singleLine="true"
                android:textSize="18dp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:paddingLeft="3dp"
                android:paddingRight="3dp"
                android:textColor="@color/black"
                android:layout_marginTop="7dp"
                android:layout_marginBottom="7dp"
                android:shadowColor="#4b000000"
                android:shadowDy="1"
                android:shadowDx="1"
                android:shadowRadius="1"
                android:cursorVisible="true"
                android:textCursorDrawable="@null"
                android:gravity="center_vertical|left"
                android:background="@color/transparent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                tools:ignore="UnusedAttribute">
            <requestFocus/>
        </EditText>


mEditText.setImeActionLabel("搜索", EditorInfo.IME_ACTION_SEARCH);
    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                MToastUtil.show("搜索");
            }
            return false;
        }
    });
于 2015-04-12T16:19:51.373 回答
0

确保:

inputType = "文本";

imeOptions = "actionSend";

于 2016-01-29T15:58:28.457 回答
-1

经过一番搜索 - 这就是我的解决方案(也适用于片段):

只需删除imeAction

于 2015-06-30T20:38:22.930 回答