如何确定当前聚焦的视图?

112

我需要找出在Activity中是否有任何聚焦的视图以及它是哪个视图。如何做到这一点?

6个回答

136

1
由于某种原因,在使用“next”操作循环遍历所有子视图后,它返回了null。 - WindRider
10
顺便提一下,getCurrentFocus() 是活动 (activity) 的一个方法,而不是视图 (view) 的。 - ToolmakerSteve
3
因此在代码片段中,我们可以使用 getActivity().getCurrentFocus().clearFocus(),例如... - Martin Pfeffer
1
可以从视图中获取托管Activity,并调用getCurrentFocus(),但不是很可靠。 - Eido95
1
Kotlin:在 Fragment 中 - activity?.currentFocus - Mohammad Reza Khahani
从视图中:(v.context as? Activity)?.currentFocus - CoolMind

13

尝试使用thread将所有内容放置其中,并实时将 id classname 打印到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: \t" + idName + "\tClass: \t" + 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: \t" + idName + "\tClass: \t" + newView.javaClass)
                }
                try {
                    Thread.sleep(100)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
        }).start()

请注意,此线程每100毫秒运行一次,以避免不必要的工作过载CPU。


你可以扩展这段代码并添加一个高亮,以便您可以看到哪个项目被选中。只要该项目在屏幕内,它就应该能够正常工作。但是使用logcat更加安全。 - Haroun Hajem

13

从活动来源:

   /**
     * 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;
    }

10
如果你在一个碎片中,你可以使用以下方法:
getView().findFocus()

6

由于某些原因,getCurrentFocus()方法不再可用;可能已经被弃用了,在这里提供一个可用的替代方案:

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

1
这是两种不同的方法。getCurrentFocus() 是一个 Activity 类的方法,而 getFocusedChild() 属于 View 类。 - BoredT
2
@BoredT:getFocusedChild()ViewGroup的一个方法。 - gnuf

1

ViewGroup有一个非常方便的方法,可以检索到当前被聚焦的子视图:

ViewGroup.getFocusedChild()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接