마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

recyclerView에서 EditText 키보드의 next / done 버튼을 클릭했을때 다음 editText로 포커스 이동.. 제발...

0 추천

제가 CustomView를 다음과 같이 만들어두었습니다.

<RelativeLayout>

    <EditText>

    <NumberPicker>

    <ImageView>

</RelativeLayout>

 

그리고 커스텀뷰를 포함하고 있는 리사이클러 아이템뷰를 생성했습니다.

 

<LinearLayout>

    <CustomView>

    <CustomView>

    <CheckBox>

</LinearLayout>

 

저는 여기서 editText의 입력이 끝났을때, doneButton or NextButton을 소프트키보드에서 터치하면 다음 editText로 포커스가 이동하는걸 구현하려고 합니다.

 

https://stackoverflow.com/questions/48035761/change-focus-sequence-on-next-press-button-in-recyclerview

이 질문과 완전 흡사해요 ㅠㅠ

혹시 포커스를 다음으로 넘기는 방법을 아시는 고수님 계실까요? 일주일째 잡고있습니다 ㅠ 제발 도와주세요

 

제가 시도해봤지만 실패한 방법은 다음과 같습니다..

1.

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/workout_today_routine_recycler"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:focusableInTouchMode="true"
    android:descendantFocusability="beforeDescendants"
    />

+ manifest에 softInputMode 추가

android:windowSoftInputMode="stateHidden|adjustPan"

 

결과 : 다음버튼 클릭시 키보드 사라짐 + 포커스 이동 안함

 

2. editText에 속성주기

<EditText
    android:layout_width="72dp"
    android:layout_height="36dp"
    android:textSize="15dp"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:text="1"
    android:singleLine="true"
    android:maxLines="1"
    android:inputType="number"
    android:background="@null"
    android:imeOptions="actionNext"
    />

 

결과 : 다음버튼 클릭시 키보드 사라짐 + 포커스 이동 안함

 

3. onBindViewHolder에서 코드로 접근

 

    public void setFocusNextView(View nextView){
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId==EditorInfo.IME_ACTION_DONE || actionId==EditorInfo.IME_ACTION_NEXT){
                    mEditText.clearFocus();
                    if (nextView != null){
                        nextView.setFocusable(true);
                        nextView.requestFocus();

                        if (nextView instanceof EditText) ((EditText)nextView).setSelection(((EditText)nextView).getText().toString().length());
                    }
                }
                return false;
            }
        });
    }

 

결과 : 다음버튼 클릭시 키보드 사라짐 + 포커스 이동 안함

 

4. onBindViewHolder에서 코드로 접근

mEditTextView.setNextFocusDownId(nextViewId);

 

5. 그 외에도 attr 을 받아와서 xml에서 다음과 같은 속성을 적용해봤는데도 변화가 없었습니다...

android:nextFocusRight="@id/custom_number_picker_second"

 

혹시라도 비슷한 문제를 해결해보신 분 계실까요? 

리사이클러뷰 안에서 포커스 관리... 너무 어렵네요ㅠ

 

 

helloAndroid12 (150 포인트) 님이 2021년 7월 19일 질문

1개의 답변

0 추천
 
채택된 답변
테스트해 보면 커스텀 뷰를 쓰더라도 안드로이드 시스템이 알아서 actionNext인 경우 다음 EditText로 포커스를 넘겨주도록 기본적으로 처리를 해주고 있습니다. 혹 커스텀뷰의 레이아웃이나 거기에 달린 리스너의 문제는 아닌지 살펴보시고요, API level이나 혹 디바이스에 테스트 하신다면 기본동작이 변경될 가능성도 있어 보입니다.

원인을 쉽게 찾으려면 일단 커스텀 뷰에서 시스템의 기본 동작이 동작될 수 있도록 아무런 추가 동작도 하지 않게 만드신 다음, 추가하신 기능을 하나씩 살려서 어디가 문제인지 찾으시는 것도 방법일 듯 싶습니다.
spark (224,800 포인트) 님이 2021년 7월 19일 답변
helloAndroid12님이 2021년 7월 20일 채택됨
답변 남겨주셔서 정말 감사합니다 !!
제가 editText에서 setOnActionListener를 받아와서 엔터키를 클릭시 기능 수행을 시켜둬서 그것때문에 기본 동작이 작동되지 않는 것 같습니다..

말씀 주신대로 기본 동작이 잘 되는지 확인해가면서 추가 기능들을 하나씩 넣어보겠습니다. ㅠㅠ !
OnEditorActionListener를 말씀하시는 거죠.
https://developer.android.com/reference/android/widget/TextView#setOnEditorActi
onListener(android.widget.TextView.OnEditorActionListener)
이 리스너의 onEditorAction 메소드는 리턴값에 대한 설명이 이렇게 나와 있습니다.

Return true if you have consumed the action, else false.

님이 해당 이벤트에 필요한 처리를 했기 때문에 시스템이 기본동작을 처리를 하지 않길 원하면 true를 리턴하시고 그렇지 않고 여전히 시스템에게 필요한 처리를 위임하실 거면 마지막에 false를 리턴하시면 됩니다.
아마도 true를 리턴하고 계셔서 기본적인 포커스의 동작이 처리가 되지 않는 걸로 보이네요.
...