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

烂漫一生 提交于 2019-12-21 22:38:51

问题


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!


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/7994772/edittext%c2%b4s-androidnextfocusdown-attibute-stops-working-when-onclick-is-set

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