EditText input method action not working when setting imeActionLabel

拟墨画扇 提交于 2019-12-01 17:44:57

问题


I have an Edittext with imeoptions asactiongo. and I triggered my event when pressing soft keyboard enter button.

mModelId.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
           // if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            if (actionId == EditorInfo.IME_ACTION_GO) {

                id = mModelId.getText().toString();
                System.out.println("Model id in Edittext:-"+ id);
                Toast.makeText(getActivity(), "You entered "+id, Toast.LENGTH_LONG).show();
                System.out.println("Before Call Volley");
                callVolley();
                handled = true;
            }
            return handled;
        }
    });

Everything works fine but when I add actionlabel to enter key the event is not firing. mModelId.setImeActionLabel("Search Model", KeyEvent.KEYCODE_ENTER);. What may be the problem?


回答1:


try this

declare edittext and OnEditorActionListener() like this

mModelId = (EditText) findViewById(R.id.edittext_id);
mModelId.setImeActionLabel("Search Model", KeyEvent.KEYCODE_ENTER);
mModelId.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         boolean handled = false;

         if (actionId == KeyEvent.KEYCODE_ENTER) {

              id = mModelId.getText().toString();
              Toast.makeText(getActivity(), "You entered "+id,    Toast.LENGTH_LONG).show();
              callVolley();
              handled = true;
            }
            return handled;
        }
});

and you use imeoptions as actionGo then revome it, i think it override ImeActionLabel once try this and reply




回答2:


I've checked at Android 2.1 and Android 4.0 versions and your code is working fine. IME_ACTION_GO event is reported in case EditText has singleLine option specified to true. In case it is specified to false actionId has IME_NULL value independently of the setImeActionLabel was called or no.

In the TextView.onKeyDown method i've found that IME_NULL actionId is used when KEYCODE_ENTER is detected

mEditor.mInputContentType.onEditorActionListener.onEditorAction(
                                this, EditorInfo.IME_NULL, event))

Perhaps it is custom keyboard issue. Do you use any? If so try these changes:

instead of

mModelId.setImeActionLabel("Search Model", KeyEvent.KEYCODE_ENTER);

should be

mModelId.setImeActionLabel("Search Model", EditorInfo.IME_ACTION_GO);



回答3:


setImeActionLabel take two parameters and the second int parameter should be one of the those that are in the EditorInfo class. Such as:

    EditorInfo.IME_ACTION_GO
    EditorInfo.IME_ACTION_DONE
    EditorInfo.IME_ACTION_NEXT
    ....

You cannot send there any other integer like KeyEvent.KEYCODE_ENTER

And you have to set bothimeOptions parameter and singleLine parameter in XML in order it to work. Example:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:imeOptions="actionGo"
    android:singleLine="true"/>

Here is the code that I used and it is working:

XML Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"/>

</LinearLayout>

And the basic Activity code:

    mEditText2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_GO) {

                Toast.makeText(MainActivity.this, "You entered " + v.getText().toString(), Toast.LENGTH_LONG).show();

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

    mEditText2.setImeActionLabel("Search Model", EditorInfo.IME_ACTION_GO);



回答4:


  1. Supply a value for EditorInfo.actionId used when an input method is connected to the text view.

    numberEditor.mInputContentType.onEditorActionListener.onEditorAction( this, EditorInfo.IME_NULL, event))

  2. Supply a value for EditorInfo.actionLabel used when an input method is connected to the text view.

Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character.



来源:https://stackoverflow.com/questions/26299861/edittext-input-method-action-not-working-when-setting-imeactionlabel

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