Android setOnEditorActionListener()不会触发

时间:2013-05-23 07:36:06

标签: java android ime

当我按下输入按钮时,我正试图将听众设置为EditText。但它根本没有发射。我使用Android 4.2.2在LG Nexus 4上进行了测试。 setOnEditorActionListener适用于使用Android 2.3的亚马逊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;
        }  
    });

我做错了什么?我该如何解决这个问题?

9 个答案:

答案 0 :(得分:7)

您可以使用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("TEST RESPONSE", "Enter was pressed");
            }
        }
    });

答案 1 :(得分:7)

确保在布局文件中设置了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

答案 2 :(得分:2)

对我有用的是,我在下面的行中添加了EditText

4

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

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

android:imeOptions="actionSend"

答案 3 :(得分:2)

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

答案 4 :(得分:1)

我使用以下代码

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

答案 5 :(得分: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;
        }
    });

答案 6 :(得分:0)

我知道了,它适用于Activity中的Edittext,但不适用于Fragement。

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

答案 7 :(得分:0)

确保:

inputType =&#34; text&#34 ;;

imeOptions =&#34; actionSend&#34;;

答案 8 :(得分:-1)

经过一些搜索 - 这是我的解决方案(works also in a fragment):

只需删除imeAction

即可