Moving focus from one EditText to another

二次信任 提交于 2019-12-08 05:14:07

问题


I designed a layout for a create account activity.

when one EditText loses focus, I'm doing some checking on the input and if something is not right I want to regain the focus on that EditText.

I already saw some questions about this, but its not working for me (or I'm doing something wrong).

when I touch on a different EditText which triggers the requestFocus(), the focus remains on the current EditText and another focus icon is shown in the EditText which requeted the focus.

help please

here's my layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.cellap.tq"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusable="true" 
android:focusableInTouchMode="true"
android:background="@drawable/create_account_background" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_marginTop="70dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp">

    <TextView 
        android:id="@+id/userTextView"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textColor="#F4DFA7"
        android:layout_marginRight="5dp"
        android:text="Username"
        android:gravity="right"
        android:layout_gravity="center_vertical"/>

    <EditText
        android:id="@+id/userEditText"
        android:inputType="text|textEmailAddress"
        android:layout_width="fill_parent"

        android:layout_height="30dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:maxLength="64"
        android:background="@drawable/edit_text"
        android:layout_gravity="center_vertical|right"
        android:textColor="@android:color/white">

    </EditText>
</LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true" 
        android:focusableInTouchMode="true" 
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp">

        <TextView 
            android:id="@+id/passTextView"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#F4DFA7"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:text="Password"/>

        <EditText
            android:id="@+id/passEditText"
            android:layout_width="fill_parent"
            android:layout_height="30dp"

            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:maxLength="30"
            android:layout_gravity="center_vertical"
            android:inputType="textPassword"
            android:textColor="@android:color/white"
            android:background="@drawable/edit_text">


        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp">

        <TextView 
            android:id="@+id/passConfirmTextView"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#F4DFA7"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:text="Confirm password"/>

        <EditText
            android:id="@+id/passConfirmEditText"
            android:layout_width="fill_parent"
            android:layout_height="30dp"

            android:layout_gravity="center_vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:maxLength="30"
            android:inputType="textPassword"
            android:background="@drawable/edit_text"
            android:textColor="@android:color/white">


        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp">

        <TextView 
            android:id="@+id/emailTextView"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#F4DFA7"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:text="Email"/>

        <EditText
            android:id="@+id/emailEditText"
            android:layout_width="fill_parent"
            android:layout_height="30dp"

            android:layout_gravity="center_vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:maxLength="255"
            android:scrollHorizontally="false" 
            android:inputType="textEmailAddress"
            android:background="@drawable/edit_text"
            android:textColor="@android:color/white">


        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp">

        <TextView 
            android:id="@+id/confirmEmailTextView"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#F4DFA7"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:text="Confirm email"/>

        <EditText
            android:id="@+id/confirmEmailEditText"
            android:layout_width="fill_parent"
            android:layout_height="30dp"

            android:layout_gravity="center_vertical"
            android:maxLength="255"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:inputType="textEmailAddress"
            android:background="@drawable/edit_text"
            android:textColor="@android:color/white">


        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp">

        <TextView 
            android:id="@+id/signupTextView"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#F4DFA7"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:text="Sign up:"/>

        <TextView 
            android:id="@+id/signUpText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#FF0000"
            android:paddingRight="5dp"

            />



    </LinearLayout>


    <com.cellap.tq.ButtonView
        android:id="@+id/create_btn"
         android:layout_width="216dp"
        android:layout_height="49dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        custom:button="create_btn"/>


    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="horizontal"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp">

        <Button 
            android:id="@+id/fbconnect_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_marginRight="5dp"
            android:background="@drawable/fbconnect_btn"/>

    </LinearLayout>

in the code I do like that:

final EditText userEditText = (EditText)findViewById(R.id.userEditText);

    userEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                Log.i(TAG, "userEditText lost focus");

                m_userName = userEditText.getText().toString();
                if(m_userName.length() < 6){
                    m_signUpText.setText("Username should have at least 6 characters");
                    userEditText.requestFocus();
                }
                else{
                    checkUserNameExists();
                }

            }
        }
    });

回答1:


You can listen on this action EditorInfo.IME_ACTION_NEXT for the first EditText and then request focus to the second one by this call requestFocus(), this is an example:

firstEditText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {

            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                secondEditText.requestFocus();
                return true;
            }
            return false;
        }
    });



回答2:


found a solution here

This solution works (no need to add android:focusable="true"\android:focusableInTouchMode="true"):

final EditText userEditText = (EditText)findViewById(R.id.userEditText);

userEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus){
            Log.i(TAG, "userEditText lost focus");
            if(null == m_requestFocus){
                m_userName = userEditText.getText().toString();
                if(m_userName.length() < 6){
                    m_signUpText.setText("Username should have at least 6 characters");
                    m_requestFocus = userEditText;
                }
                else{
                    checkUserNameExists();
                }
            }
        }
        else{
            if(null != m_requestFocus & m_requestFocus != userEditText){
                v.clearFocus();
                m_requestFocus.requestFocus();
                m_requestFocus = null;
            }
        }
    }
});

To clarify the problem:

User edits EditText A -> user touch EditText B to edit it -> EditText A onFocusListener.onFocusChanged is called -> EditText A requestFocus -> EditText B still has the focus and typing any text, writes to its edit text. only a marker of focus, appears on EditText A.




回答3:


First of all why do you provide

android:focusable="true" android:focusableInTouchMode="true"

to layout, not to EditText field?

Second - requestFocus() definition from dev.andro: "Call this to try to give focus to a specific view or to one of its descendants."

Conclusion will be a question, because I poorly understood what you've said. This is working, focus is staying in userEditText but some other EditText is having selector for focused View?




回答4:


This works for me, and it looks less hackish.

What is done is send the requestFocus() in a Runnable to the process message queue. Differently from the link above, I didn't need to clear the focus of the other field and didn't need to postDelayed. My code

        new Handler().post(new Runnable() {

              @Override
              public void run() {
                   ev1.requestFocus();
              }
        });


来源:https://stackoverflow.com/questions/8630819/moving-focus-from-one-edittext-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!