초점이 맞춰진보기를 찾는 방법은 무엇입니까?


87

어떤 뷰가 Activity 내부에 초점이 맞춰져 있는지, 어떤 뷰인지 알아 내야합니다. 어떻게하나요?

답변:


114

전화 getCurrentFocus()활동에.


어떤 이유로 "다음"작업으로 모든 자식 뷰를 순환 한 후 null을 반환합니다.
WindRider

10
BTW, getCurrentFocus ()는보기가 아닌 활동의 방법입니다.
ToolmakerSteve 2015

3
... 그래서 조각에 우리는 getActivity ()를 사용할 수 getCurrentFocus을 () 예 .. clearFocus ()..
마틴 페퍼

View에서 호스팅 활동가져 와서을 호출 하는 것은 가능 getCurrentFocus()하지만 신뢰할 수는 없습니다.
Eido95

kotlin : 조각에서activity?.currentFocus
Mohammad Reza Khahani

12

활동 소스에서 :

   /**
     * Calls {@link android.view.Window#getCurrentFocus} on the
     * Window of this Activity to return the currently focused view.
     * 
     * @return View The current View with focus or null.
     * 
     * @see #getWindow
     * @see android.view.Window#getCurrentFocus
     */
    public View getCurrentFocus() {
        return mWindow != null ? mWindow.getCurrentFocus() : null;
    }

6

어떤 이유로 getCurrentFocus () 메서드를 더 이상 사용할 수 없습니다. 아마도 이미 사용되지 않을 것입니다. 여기에 작동하는 대안이 있습니다.

View focusedView = (View) yourParentView.getFocusedChild();

1
두 가지 다른 방법입니다. getCurrentFocus ()는 Activity 클래스 메서드이고 getFocusedChild ()는 View 클래스에 속합니다
BoredT

2
@BoredT : getFocusedChild()ViewGroup.
gnuf

5

, 안쪽이 대신 넣어 모든 것을 시도 thread하고 인쇄 ID를 하고 클래스 이름은 에 살고 logcat. 이 코드 Activity를, onCreate메서드에 넣은 다음 logcat현재 초점이 맞춰진 것을 확인하십시오.

자바

  new Thread(() -> {
        int oldId = -1;
        while (true) {
            View newView= this.getCurrentFocus();
            if (newView!= null && newView.getId() != oldId) {
                oldId = view.getId();
                String idName = "";
                try {
                   idName = getResources().getResourceEntryName(newView.getId());
                 } catch (Resources.NotFoundException e) {
                   idName = String.valueOf(newView.getId());
                 }
                Log.i(TAG, "Focused Id: " + idName + " Class: " + newView.getClass());
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

코 틀린

      Thread(Runnable {
            var oldId = -1
            while (true) {
                val newView: View? = this.currentFocus
                if (newView != null && newView.id != oldId) {
                    oldId = newView.id
                    var idName: String = try {
                        resources.getResourceEntryName(newView.id)
                    } catch (e: Resources.NotFoundException) {
                        newView.id.toString()
                    }
                    Log.i(TAG, "Focused Id: " + idName + " Class: " + newView.javaClass)
                }
                try {
                    Thread.sleep(100)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
        }).start()

이 스레드는 100ms 주기로 실행되므로 불필요한 작업으로 CPU가 오버플로되지 않습니다.



1

ViewGroup에는 집중된 자식을 검색하는 매우 편리한 방법이 있습니다.

ViewGroup.getFocusedChild()
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.