どのビューが焦点を合わせているかを見つける方法は?


87

アクティビティ内でフォーカスされているビューがあるかどうか、およびそれがどのビューであるかを確認する必要があります。これを行う方法?

回答:


114

getCurrentFocus()アクティビティを呼び出します。


何らかの理由で、「次の」アクションですべての子ビューを循環させた後、nullを返します。
WindRider 2014

10
ところで、getCurrentFocus()はアクティビティのメソッドであり、ビューではありません。
toolmakerSteve 2015年

3
...フラグメントでは、getActivity()。getCurrentFocus()。clearFocus()を使用できます。例:
Martin Pfeffer

ビューと呼び出しからホスティングアクティビティ取得することは可能ですgetCurrentFocus()が、それほど信頼できるものではありません。
Eido95 2016

kotlin:フラグメントで-activity?.currentFocus
モハマド・レザ・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
これは2つの異なる方法です。getCurrentFocus()はActivityクラスのメソッドであり、getFocusedChild()はViewクラスに属しています
BoredT 2015年

2
@BoredT:はのgetFocusedChild()メソッドViewGroupです。
gnuf 2015年

5

代わりにこれを試してください。すべてをaの中に入れてthreadIDクラス名をライブでに出力してくださいlogcat。このコードをの中に入れActivityて、onCreateメソッドに入れてから、logcat現在フォーカスいるものしてください。

JAVA

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

KOTLIN

      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.