regisxp regisxp - 5 months ago 101
Android Question

EditText´s android:nextFocusDown attibute stops working when onClick is set

Does anybody knows why the android:nextFocusDown attibute stops working when we set the onClick on it?

On the example below, we have some EditText with this attribute defined:

<TableRow
android:weightSum="1"
android:gravity="center">
<EditText
android:id="@+id/txtChave1"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"
android:nextFocusDown="@+id/txtChave2"></EditText>

<EditText
android:id="@+id/txtChave2"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"
android:nextFocusDown="@+id/txtChave3"></EditText>

<EditText
android:id="@+id/txtChave3"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"
android:nextFocusDown="@+id/txtChave4"></EditText>

<EditText
android:id="@+id/txtChave4"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"></EditText>
</TableRow>


As defined above, when user clicks on the "next button" on virtual keyboard, the focus changes as expected... But, if we set the onClick attribute, the android:nextFocusDown doen´t work anymore.

<EditText
android:id="@+id/txtChave1"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"
android:nextFocusDown="@+id/txtChave2"
android:onClick="onClickHandler">
</EditText>


Any advice?

Thanks a lot!

Answer

Do it programmatically because when you call android:nextFocusDown in XML may be the object that you want to call in the nextFocus will not create it till.

private void setUpView(){
    editText1=(EditText)findViewById(R.id.editText1);
    editText2=(EditText)findViewById(R.id.editText2);
    editText3=(EditText)findViewById(R.id.editText3);
 }
private void setUpFocus(){
  editText1.setNextFocusDownId(R.id.editText2);
    editText2.setNextFocusDownId(R.id.editText8);// you can give focus to any id
    editText3.setNextFocusDownId(R.id.editText9);
 }

Call setUpView() before setUpFocus() in onCreate().