android - 如何同时拥有 singleLine ="false"和 imeOptions ="actionNext"用于 EditText?

标签 android focus android-edittext android-softkeyboard multiline

背景

假设您有多个 EditText 实例。

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

每个 EditText 可能有很长的内容,可以显示在多行中(假设我希望将其限制为 3 行,如果文本仍然太长,请使用椭圆大小)。

问题

正如我所注意到的,TextView 和 EditText 都有非常奇怪的行为并且缺乏一些基本功能。其中之一是,如果您希望转到下一个 View ,则需要为每个 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>

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

...
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;
      }
  });

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

  • 我需要为每个 EditText 设置此行为(或扩展 EditText 并在其中添加某种逻辑)。
  • 软键盘上的按键不显示“next”。相反,它显示 ENTER 键。
  • 使用 XML 不允许我在最后设置椭圆大小,我一直在滚动。

问题

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

最佳答案

我发现这个问题类似于 Multi-line EditText with Done action button邮政。既然我已经写了答案,那我再放一遍。不过,我只能回答你的前两点,因为我还没有使用Ellipsize(我认为它应该相对容易使用下一步按钮部分设置后)。

以下 Java 代码 可用于将键盘输入更改为“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;
        }
});

关于android - 如何同时拥有 singleLine ="false"和 imeOptions ="actionNext"用于 EditText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15209106/

相关文章:

c# - 如何聚焦模式对话框单击 C# 中的通知图标?

css - 制作 :focus work in ie7 using css?

java - 调用 transferFocus 还是 requestFocus?

java - 编辑文本到整数 (Android)

Android - 如何从 MainActivity 中的 RecyclerView 检索 EditText 值

android - 在某些设备中抛出 java.lang.NoClassDefFoundError,但不是全部

android - 使用 GMail 发送带有 EXTRA_STREAM 的电子邮件 Intent 会崩溃

java - Box2D Android Studio 绘制倒置形状

java - Android java 两个用于编辑文本的监听器

android - 如何等待协程 Dispatch.IO 数据库查询完成以填充 RecyclerView