我的屏幕上有几个EditTexts,它们都是按照以下方式构建的。
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());
以编程方式执行此操作,但我不能,因为我使用循环来创建EditText
s,如代码中所示。
做这个 :
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;
}
}
});
是的,您需要从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"
/>
正如你所提到的那样,你是以编程方式而不是在xml中使用它
android:nextFocusDown="@id/textInputEditTextSecond"
您可以像这样以编程方式使用它
editText.setNextFocusForwardId(editTextSecond.getId());
并为所有EditText添加editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
在每个EditText的循环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);
}
}