如何制作EditText IME" Next"如果没有正确完成,按钮不会进入下一个可聚焦的EditText

时间:2018-02-05 15:29:21

标签: android android-edittext focus ime

到目前为止,我有这样的布局:

 <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然后再回到第一个。 如果没有设置10ms的延迟,它将从第一个EditText转到第二个EditText。 期望的效果:如果有效,请转到辅助字段。如果无效,请保留在第一个字段中。

1 个答案:

答案 0 :(得分:0)

我将android:nextFocusForward="@id/sameObjectID"添加到了我的EditTexts,所以当我按下IME&#34; Next&#34;按钮,它不会改变焦点,我会用我的延迟方法改变它。因此,如果需要更改,它将在10ms后改变,如果不是,它将保持在同一视图上(并在10ms后更改为同一视图),但是像这样,从1 EditText跳转到另一个将不会再见了。 我觉得这是一个黑客,所以我可以接受其他建议