EditText´s android:nextFocusDown attibute stops wo

2019-04-16 06:56发布

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!

2条回答
你好瞎i
2楼-- · 2019-04-16 07:28

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().

查看更多
等我变得足够好
3楼-- · 2019-04-16 07:34

Had the same problem recently and found out that it will work if you use setOnTouchListener instead of setOnClickListener. In the onTouch method use MotionEvent.ACTION_DOWN or MotionEvent.ACTION_UP, according to what you need. Also don't forget to return FALSE for events that also have to be processed by the system (if you want to be safe, always return false).

查看更多
登录 后发表回答