Android edittext key return goes to next text

为君一笑 提交于 2019-11-28 17:46:58

Much simpler than sniffing keys: try setting android:singleLine="true" and android:imeOptions="actionNext" (at least for single-line entry textviews). Read more in the Android documentation for TextView.

Update: singleLine is deprecated now, but you can leave it out. It's the default behavior for editable text as long as you don't explicitly set android:inputType="textMultiLine", and inputType overrides the suggested singleLine replacement of maxLines="1" for editable text anyways.

For an alternative or a newer approach for the answer given from Yoni...

Since singleLine is considereded deprecated, we can set manually to android:maxLines="1" with android:inputType="text".

Small explanation:

  • We use android:inputType="text" to specifically treat the input as plain text.
  • And we use android:maxLines="1" to set the max lines of the text to 1 (as it suggests).

Using maxLines="1" alone, will not cause any effect, but inputType="text" alone may work also, as Adam mentions (though I haven't checked this).

Adding

    android:inputType="text"

in XML is just enough. When You are not defining input type, then it goes to next line.

You could try adding a single event listener to all your editText objects:

OnKeyListener myKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
            // TODO: do what you got to do
            return false;
        }
    };
editText1.setOnKeyListener(myKeyListener);
editText2.setOnKeyListener(myKeyListener);
editText3.setOnKeyListener(myKeyListener);

Apply this custom style in styles.xml

<style name="SingleLineText">
    <item name="android:inputType">text</item>
    <item name="android:maxLines">1</item>
</style>

and then set it in EditText like this

 style="@style/SingleLineText"
Sudhir singh

Add the following lines in EditText

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