How to use SingleLine = "false" and imeOptions = "actionNext" for EditText?

Background

Suppose you have multiple instances of EditText.

You want to be able to switch between them using the next button on the keyboard (which is used as a replacement for the ENTER key).

Each EditText can have long content that can appear in multiple lines (suppose I want to limit it to three lines, and if the text is still too long, use an ellipsis).

Problem

As I noticed, both TextView and EditText have really strange behavior and lack of some basic functions. One of them is that if you want to go to the next view, for each instance of EditText you need to have singleLine = "true".

What i tried

I tried the following xml layout (and other tests), but it does not work:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:nextFocusDown="@+id/editText2" android:singleLine="false" android:imeOptions="actionNext" android:maxLines="3" android:ellipsize="end" android:text="@string/very_long_text" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:nextFocusDown="@+id/editText3" android:singleLine="false" android:imeOptions="actionNext" android:maxLines="3" android:ellipsize="end" android:text="@string/very_long_text" /> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="false" android:maxLines="3" android:ellipsize="end" android:text="@string/very_long_text"/> </LinearLayout> 

I also tried the following code, but this is a really stupid solution:

 ... final EditText editText=(EditText)findViewById(R.id.editText1); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(final TextView v,final int actionId,final KeyEvent event) { if(actionId==EditorInfo.IME_NULL) { final View view=findViewById(editText.getNextFocusDownId()); if(view!=null) { view.requestFocus(); return true; } } return false; } }); 

This works, but it is a stupid solution due to the following reasons:

  • I need to set this behavior for each EditText (or extend the EditText and add some logic there).
  • It does not show the β€œnext” for a soft keyboard key. Instead, it shows the ENTER key.
  • Playing with XML did not allow me to set the ellipsis at the end, and I continued to scroll through the behavior.

Question

Is there a better way to achieve this? Elegant, working and showing the "next" key instead of the ENTER key?

+6
source share
2 answers

I find this question similar to a Multi-line EditText with a Finish button . Since I already wrote an answer for this, let me turn it on again. Although, I can only answer your first 2 points, because I still need to use Ellipsize (I think it will be relatively easy if you are configured using the next strong button> part).

The following Java code can be used to change the keyboard input to the "DONE" / "NEXT" button:

 ////////////Code to Hide SoftKeyboard on Enter (DONE) Press/////////////// editText1.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); editText1.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE); //Set Return Carriage as "DONE" editText1.setImeOptions(EditorInfo.IME_ACTION_DONE); editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (event == null) { if (actionId == EditorInfo.IME_ACTION_DONE) { // Capture soft enters in a singleLine EditText that is the last EditText // This one is useful for the new list case, when there are no existing ListItems editText1.clearFocus(); editText2.requestFocus(); //Call the next Edit_Text into Focus //Comment above requestFocus() for edit_text3 //Hiding SoftKeyboard. Uncomment it for edit_text3 //InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE); //inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0); } else if (actionId == EditorInfo.IME_ACTION_NEXT) { // Capture soft enters in other singleLine EditTexts } else if (actionId == EditorInfo.IME_ACTION_GO) { } else { // Let the system handle all other null KeyEvents return false; } } else if (actionId == EditorInfo.IME_NULL) { // Capture most soft enters in multi-line EditTexts and all hard enters; // They supply a zero actionId and a valid keyEvent rather than // a non-zero actionId and a null event like the previous cases. if (event.getAction() == KeyEvent.ACTION_DOWN) { // We capture the event when the key is first pressed. } else { // We consume the event when the key is released. return true; } } else { // We let the system handle it when the listener is triggered by something that // wasn't an enter. return false; } return true; } }); 
0
source

For the answer here you can install both of them if you want. However, forcing it to actually display the following and activate the enter key, the next key is keyboard dependent. I know that in Swype we intentionally redefined any text field that was multi-line, to ALWAYS display the enter key and act as a new line, and NEVER show the next key. There is no way to make this work the way you want on all keyboards.

0
source

All Articles