【问题标题】:How to have both singleLine="false" AND imeOptions="actionNext" for EditText?EditText 如何同时拥有 singleLine="false" 和 imeOptions="actionNext"?
【发布时间】:2013-02-18 23:40:48
【问题描述】:

背景

假设您有多个 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>

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

...
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 并在那里添加某种逻辑)。
  • 软键盘中的按键不显示“下一步”。而是显示 ENTER 键。
  • 在使用 XML 时,我无法在最后设置椭圆大小,并且一直出现滚动行为。

问题

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

【问题讨论】:

  • 您解决了这个问题吗?我也面临同样的问题。如果可能,请分享您的解决方案。
  • @h4ck3d 我忘记了这个。看看我制作的旧代码,似乎我只是使用了 android:singleLine="true" 。只有我看到我有没有 singleLine 的“actionNext”的地方是:github.com/AndroidDeveloperLB/ChipsLibrary,但大部分项目都是基于谷歌的库,所以我不知道这个功能是如何(甚至是否)在那里工作的。我没有时间去挖掘它,但是如果你了解那里发生的事情并发现它有助于回答这个问题,请发布它......

标签: android focus android-edittext android-softkeyboard multiline


【解决方案1】:

这里有一个答案——如果你愿意,你可以同时设置它们。然而,强制它实际显示下一个并让回车键充当下一个键取决于键盘。我知道在 Swype,我们故意覆盖了任何多行文本字段,以始终显示回车键并充当换行符,并且从不显示下一个键。没有办法强制它在所有键盘上都像你想要的那样工作。

【讨论】:

    【解决方案2】:

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

    以下Java 代码可用于将键盘输入更改为“完成”/“下一步”按钮:

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

    【讨论】:

    • 按下第一个 EditText 上的 next/done/enter 按钮后,键盘消失
    • 抱歉。忘记评论隐藏软键盘功能(保留它以供您与 edit_text3 一起使用)。已编辑,现在应该可以完美运行了。
    • 似乎可行,但您应该使用 IME_ACTION_NEXT 而不是 IME_ACTION_DONE,因为“下一个”用于转到另一个字段,而“完成”用于完成编辑。另外,我认为它可以更概括地处理所有的 EditText,通过获取下一个视图来关注(如果没有其他功能可以做到这一点,可以使用 getNextFocusRightId 和 getNextFocusDownId)。但是,由于它现在运行良好,我接受这个答案。谢谢你:)
    • 当然,它需要一些修改,因为我主要是为 DONE 案例编写的,但很高兴听到你通过它。很高兴一如既往地提供帮助。 :D
    • 您知道如何获得下一个焦点视图吗?意思是按 NEXT 应该去的那个,如果一切都按照默认规则工作?
    猜你喜欢
    • 2014-05-27
    • 1970-01-01
    • 2021-10-03
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多