如果未正确完成,如何使 EditText IME "Next" 按钮不转到下一个可聚焦的 EditText
How can I make the EditText IME "Next" button not take to the next focusable EditText, if not completed correctly
到目前为止我有这样的布局:
<RelativeLayout
android:id="@+id/email_container"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="50dp"
android:background="@android:color/transparent">
<ImageView
android:id="@+id/email_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:background="@color/white"
android:src="@drawable/icn_email_settings"/>
<RelativeLayout
android:id="@+id/checked_icon_cont"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:background="@color/white"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/checked_icon"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/white"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:src="@drawable/ic_green_checked"/>
</RelativeLayout>
<nl.hgrams.passenger.ui.EditTextBackEvent
android:id="@+id/login_email_field"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@id/email_icon"
android:layout_toLeftOf="@id/checked_icon_cont"
android:paddingLeft="20dp"
android:inputType="textEmailAddress"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:focusable="true"
android:hint="mail@example.com"
android:textSize="19dp"
android:background="@color/white"
android:textColorHint="#cccccc"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/pass_container"
android:layout_width="match_parent"
android:layout_below="@id/email_container"
android:layout_height="44dp">
<ImageView
android:id="@+id/pass_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@color/white"
android:src="@drawable/icn_password_settings"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_toRightOf="@id/pass_icon"
android:background="@color/gray_lighter"/>
<RelativeLayout
android:id="@+id/checked_icon_cont_pass"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:background="@color/white"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/checked_icon_pass"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/white"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:src="@drawable/ic_green_checked"/>
</RelativeLayout>
<nl.hgrams.passenger.ui.EditTextBackEvent
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:layout_marginLeft="2dp"
android:paddingLeft="18dp"
android:layout_toLeftOf="@id/checked_icon_cont_pass"
android:layout_toRightOf="@id/pass_icon"
android:ems="10"
android:background="@color/white"
android:textColorHint="#cccccc"
android:layout_centerVertical="true"
android:id="@+id/passwordEditField"
android:layout_gravity="center_horizontal"
android:hint="Password"/>
</RelativeLayout>
还有这段代码:
TextView.OnEditorActionListener doneListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
if (isEmailValid(emailField.getText().toString()) && validatePassword(passwordEditField.getText().toString())){
performEmailCheck();
}else requestFocusDelayed(passwordEditField);
}
return false;
}
};
EditTextImeBackListener listener = new EditTextImeBackListener() {
@Override
public void onImeBack(EditTextBackEvent ctrl, String text) {
Log.i("", "closed keyboard");
base.findViewById(R.id.container).performClick();
}
};
emailField.setOnEditTextImeBackListener(listener);
emailField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
if (isEmailValid(emailField.getText().toString())){
requestFocusDelayed(passwordEditField);
}else requestFocusDelayed(emailField);
}
return false;
}
});
passwordEditField.setOnEditTextImeBackListener(listener);
passwordEditField.setOnEditorActionListener(doneListener);
public void requestFocusDelayed(final EditTextBackEvent view){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
view.requestFocus();
}
},10);
}
现在可以了。但是可以看到焦点线是如何从第一个 EditText 转到第二个 EditText 然后再回到第一个的。
在不设置 10 毫秒延迟的情况下,它会从第一个 EditText 转到第二个 EditText。
预期效果:如果有效,则转到辅助字段。如果无效,则保留在第一个字段上。
我将 android:nextFocusForward="@id/sameObjectID"
添加到我的 EditTexts,所以当我按下 IME "Next" 按钮时,它不会改变焦点,然后我会用我的延迟方法改变它。因此,如果需要更改,它将在 10 毫秒后更改,如果不需要,它将保持在同一视图(并在 10 毫秒后更改为同一视图),但像这样,从 1 个 EditText 跳转到另一个不会可见了。
我觉得这是一个 hack,所以我愿意接受其他建议
到目前为止我有这样的布局:
<RelativeLayout
android:id="@+id/email_container"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="50dp"
android:background="@android:color/transparent">
<ImageView
android:id="@+id/email_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:background="@color/white"
android:src="@drawable/icn_email_settings"/>
<RelativeLayout
android:id="@+id/checked_icon_cont"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:background="@color/white"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/checked_icon"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/white"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:src="@drawable/ic_green_checked"/>
</RelativeLayout>
<nl.hgrams.passenger.ui.EditTextBackEvent
android:id="@+id/login_email_field"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@id/email_icon"
android:layout_toLeftOf="@id/checked_icon_cont"
android:paddingLeft="20dp"
android:inputType="textEmailAddress"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:focusable="true"
android:hint="mail@example.com"
android:textSize="19dp"
android:background="@color/white"
android:textColorHint="#cccccc"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/pass_container"
android:layout_width="match_parent"
android:layout_below="@id/email_container"
android:layout_height="44dp">
<ImageView
android:id="@+id/pass_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@color/white"
android:src="@drawable/icn_password_settings"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_toRightOf="@id/pass_icon"
android:background="@color/gray_lighter"/>
<RelativeLayout
android:id="@+id/checked_icon_cont_pass"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:background="@color/white"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/checked_icon_pass"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/white"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:src="@drawable/ic_green_checked"/>
</RelativeLayout>
<nl.hgrams.passenger.ui.EditTextBackEvent
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:layout_marginLeft="2dp"
android:paddingLeft="18dp"
android:layout_toLeftOf="@id/checked_icon_cont_pass"
android:layout_toRightOf="@id/pass_icon"
android:ems="10"
android:background="@color/white"
android:textColorHint="#cccccc"
android:layout_centerVertical="true"
android:id="@+id/passwordEditField"
android:layout_gravity="center_horizontal"
android:hint="Password"/>
</RelativeLayout>
还有这段代码:
TextView.OnEditorActionListener doneListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
if (isEmailValid(emailField.getText().toString()) && validatePassword(passwordEditField.getText().toString())){
performEmailCheck();
}else requestFocusDelayed(passwordEditField);
}
return false;
}
};
EditTextImeBackListener listener = new EditTextImeBackListener() {
@Override
public void onImeBack(EditTextBackEvent ctrl, String text) {
Log.i("", "closed keyboard");
base.findViewById(R.id.container).performClick();
}
};
emailField.setOnEditTextImeBackListener(listener);
emailField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
if (isEmailValid(emailField.getText().toString())){
requestFocusDelayed(passwordEditField);
}else requestFocusDelayed(emailField);
}
return false;
}
});
passwordEditField.setOnEditTextImeBackListener(listener);
passwordEditField.setOnEditorActionListener(doneListener);
public void requestFocusDelayed(final EditTextBackEvent view){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
view.requestFocus();
}
},10);
}
现在可以了。但是可以看到焦点线是如何从第一个 EditText 转到第二个 EditText 然后再回到第一个的。 在不设置 10 毫秒延迟的情况下,它会从第一个 EditText 转到第二个 EditText。 预期效果:如果有效,则转到辅助字段。如果无效,则保留在第一个字段上。
我将 android:nextFocusForward="@id/sameObjectID"
添加到我的 EditTexts,所以当我按下 IME "Next" 按钮时,它不会改变焦点,然后我会用我的延迟方法改变它。因此,如果需要更改,它将在 10 毫秒后更改,如果不需要,它将保持在同一视图(并在 10 毫秒后更改为同一视图),但像这样,从 1 个 EditText 跳转到另一个不会可见了。
我觉得这是一个 hack,所以我愿意接受其他建议