如何为EditText同时使用singleLine ="false"和imeOptions ="actionNext"?

and*_*per 6 android focus multiline android-softkeyboard android-edittext

背景

假设您有多个EditText实例.

您希望能够使用键盘的下一个按钮(用作ENTER键的替换)在它们之间切换.

每个EditText可能都有一个可以多行显示的长内容(假设我希望将其限制为3行,如果文本仍然太长,请使用ellipsize).

问题

正如我所注意到的,TextView和EditText都有非常奇怪的行为,缺乏一些基本功能.其中之一是,如果您希望转到下一个视图,则需要为每个EditText实例设置singleLine ="true".

我试过的

我已经尝试了下一个xml布局(和其他试验),但它不起作用:

<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>
Run Code Online (Sandbox Code Playgroud)

我也尝试了下一个代码,但它确实是一个愚蠢的解决方案:

...
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;
      }
  });
Run Code Online (Sandbox Code Playgroud)

它有效,但由于下面的原因,这是一个愚蠢的解决方案:

  • 我需要为每个EditText设置此行为(或扩展EditText并在那里添加某种逻辑).
  • 它不会显示软键盘中键的"下一步".而是显示ENTER键.
  • 使用XML不允许我在最后设置ellipsize并且我不断获得滚动行为.

问题

有没有更好的方法来实现这一目标?一个优雅,工作,并显示"下一个"键而不是ENTER键?

Kau*_* NP 0

我发现这个问题类似于带有完成操作按钮的多行 EditText帖子。既然我已经写过答案了,那我再补充一下吧。不过,我只能回答你的前两点,因为我还没有使用Ellipsize (我认为一旦你设置了下一个按钮部分,它应该相对容易)。

以下Java 代码可用于将键盘 Enter 更改为“DONE”/“NEXT”按钮:

////////////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;
        }
});
Run Code Online (Sandbox Code Playgroud)