如何找出焦点所在?


87

我需要找出是否有一个活动集中在任何视图,它是什么视图。这该怎么做?

Answers:


114

调用getCurrentFocus()活动。


由于某种原因,在使用“ next”操作循环所有子视图之后,它返回null。
WindRider 2014年

10
顺便说一句,getCurrentFocus()是一种活动方法,而不是视图方法。
制造商史蒂夫(Steve)2015年

3
...因此,在片段中,我们可以使用getActivity()。getCurrentFocus()。clearFocus()例如。–
Martin Pfeffer

可以从View and call获得托管ActivitygetCurrentFocus(),但不够可靠。
Eido95 '16

科特林:在片段-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
这是两种不同的方法。getCurrentFocus()是Activity类的方法,而getFocusedChild()属于View类
BoredT 2015年

2
@BoredT:getFocusedChild()是的方法ViewGroup
gnuf 2015年

5

改用此方法,将所有内容放到a内thread,并实时将idclassname打印到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()
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.