【问题标题】:Make next EditText appear on screen when action next clicked下次单击操作时,使下一个 EditText 出现在屏幕上
【发布时间】:2019-03-20 10:50:37
【问题描述】:

我的屏幕上有几个 EditText,它们都是按以下方式构建的。

XML:

<android.support.design.widget.TextInputEditText
            android:id="@+id/textInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/name"
            app:fontFamily="@font/karla"
            android:inputType="textMultiLine|textCapCharacters"
            android:maxLength="12"
            android:textColor="#5C7264"
            />

代码:

private void addItemsToLinearLayout(int numItems) {

 LayoutInflater inflater = LayoutInflater.from(mContext);

     for (int i = 0; i < numItems; i++) {

         View childView = inflater.inflate(R.layout.item_linearlayout, null);        

         linearlayoutPlayersNames.addView(childView);            

         ImageView deleteRowIcon = childView.findViewById(R.id.deleteRowIcon);     
         deleteRowIcon.setOnClickListener(this);

         TextView myTV = childView.findViewById(R.id.txtTitle);          
         myTV.setText(String.format(getString(R.string.player), linearlayoutPlayersNames.indexOfChild(childView) + 1));         


         EditText editText = childView.findViewById(R.id.textInputEditText);
         editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
         editText.setRawInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

    }
}

当我单击软键盘的下一个按钮时,它会按预期移动到下一个 EditText,但前提是下一个 EditText 在屏幕上可见。当下一个 EditText 在屏幕上不可见时,下一个操作不起作用,最重要的是,它会将下一个操作按钮更改为回车操作按钮,如以下视频所示:https://imgur.com/a/snAbdmU

我打算做的是单击下一个按钮并将可见性移动到下一个 EditText。

更新:你们中的一些人告诉我使用android:nextFocusDown 或以编程方式使用editText.setNextFocusForwardId(editTextSecond.getId()); 执行此操作,但我不能,因为我使用循环来创建EditTexts,如图所示现在在代码中。

【问题讨论】:

  • 你在你的 xml 中试过 android:imeOptions="actionNext" 吗?
  • @AntonisRadz 如您所见,它是以编程方式设置的。当下一个 EditText 在屏幕上不可见时,就会出现不当行为。
  • 没有其他解决方案@AbidKhan 吗?我根据想要玩的玩家数量以编程方式生成 EditTexts,所以这对我来说并不容易。
  • 使用edittext.setSingleLine(true);edittext.setMaxLines(1);检查
  • 然后在我的答案中检查它。

标签: android android-edittext android-softkeyboard android-inputtype


【解决方案1】:

这样做:

  EditText editText2 = childView.findViewById(R.id.textInputEditText);
  EditText editText = childView.findViewById(R.id.textInputEditText);
  editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE) {
        editText2.setVisibility(View.VISIBLE);
        editText2.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        editText2.setRawInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
              return handled;
             }
        }
    });

  editText2.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE) {
               // do same for others to    
            return handled;  
             }

        }
    });

【讨论】:

    【解决方案2】:

    是的,您需要从 android:inputType 中删除 textMultiLine,然后它将适用于下一个 EditText 可见和不可见的两种情况。

    <android.support.design.widget.TextInputEditText
            android:id="@+id/textInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/name"
            app:fontFamily="@font/karla"
            android:inputType="textCapCharacters"
            android:maxLength="12"
            android:textColor="#5C7264"
            />
    

    【讨论】:

    • 我想使用 textMultiLine... 即使这样做也不能解决问题。
    【解决方案3】:

    正如你提到的,你是以编程方式而不是在 xml 中使用

    android:nextFocusDown="@id/textInputEditTextSecond"
    

    您可以像这样以编程方式使用它

    editText.setNextFocusForwardId(editTextSecond.getId());
    

    并为所有 EditText 添加editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);

    在每个EditText的for循环中setId,然后设置nextForward如下

    private void addItemsToLinearLayout(int numItems) {
    
     LayoutInflater inflater = LayoutInflater.from(mContext);
    
         for (int i = 0; i < numItems; i++) {
    
             View childView = inflater.inflate(R.layout.item_linearlayout, null);        
    
             linearlayoutPlayersNames.addView(childView);            
    
             ImageView deleteRowIcon = childView.findViewById(R.id.deleteRowIcon);     
             deleteRowIcon.setOnClickListener(this);
    
             TextView myTV = childView.findViewById(R.id.txtTitle);          
             myTV.setText(String.format(getString(R.string.player), linearlayoutPlayersNames.indexOfChild(childView) + 1));         
    
    
             EditText editText = childView.findViewById(R.id.textInputEditText);
             editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
             editText.setRawInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
    
             // setId of each EditText and then set nextForward as
             editText.setId(i);
             editText.setNextFocusForwardId(i+1);
    
        }
    }
    

    【讨论】:

    • 感谢您的帮助,但我对问题的更新显示,我的情况有点复杂。
    • 再次感谢您,但它也不适用于我的情况......我想对其他人可能会有所帮助,但对我没有帮助。我有一个更复杂的布局,一旦用户选择了玩家的数量,他也可以删除和添加更多玩家,因此在删除玩家并添加新玩家之后,这些新玩家 ID 不会是连续的,因此 .setNextFocusForwardId 的行为将是出乎意料。
    猜你喜欢
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多