setOnEditorActionListener is not working for Android Lollipop

喜欢而已 提交于 2019-12-23 03:40:59

问题


I have been trying this IME_ACTION

 //Listening to the keyboard action
    mSearchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == R.id.ime_search || actionId == EditorInfo.IME_ACTION_SEARCH) {

                performSearch();
                return true;
            }
            return false;
        }
    });

but seems like it doesn't at all work on Lollipop devices.

Here is the XML code - I am completely sure that I am doing this right.

 <org.mapunity.widget.FloatingEditText
        android:id="@+id/fragment_search_edit_text"
        android:hint="Enter a Text to Search"
        android:layout_marginTop="@dimen/spacing_small"
        android:layout_marginBottom="@dimen/spacing_small"
        android:singleLine="true"
        app:floating_edit_text_highlighted_color="@color/color_primary"
        android:layout_marginLeft="@dimen/spacing_normal"
        android:imeActionLabel="@string/action_search"
        android:imeOptions="actionSearch"
        android:imeActionId="@+id/ime_search"
        android:inputType="text"
        android:layout_marginRight="@dimen/spacing_normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <requestFocus></requestFocus>
        </org.mapunity.widget.FloatingEditText>

Please provide an input:

I know there are a lot of similar questions but mine is specifically about Lollipop, i.e. Android 5.0+.


回答1:


I had the same problem that you did. So, I've done some testing with the devices that I have.

The results indicate that devices with API > 19 don't respond to IME_ACTION

So, the solution is just to remove the if statement in your code:

mSearchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        performSearch();
        return true;
    }
});


来源:https://stackoverflow.com/questions/29765009/setoneditoractionlistener-is-not-working-for-android-lollipop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!