下一步单击操作时,使下一个EditText出现在屏幕上

时间:2019-03-20 10:50:38

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

我的屏幕上有几个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());进行编程,但是我做不到,因为我使用循环来创建{{ 1}},如代码中所示。

3 个答案:

答案 0 :(得分:0)

执行此操作:

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

        }
    });

答案 1 :(得分:0)

是的,您需要从textMultiLine中删除android:inputType,然后在下一个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"
        />

答案 2 :(得分:0)

正如您提到的那样,您正在以编程方式进行操作,而不是使用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);

    }
}