Can two views both have focus in Android

限于喜欢 提交于 2019-12-02 02:01:11

问题


I have a EditText, and a popupWindown(which has a ListView in it), now i touch the EditText, the popupWindow shows.

i need the situation: i can input something in SoftInput, and i also can click a child item in ListView which is the children of PopupWindow.

Now, i can only input, or can only click listview , i can't do the both thing in Android 4.1, 4.2. But i can do both in Android 4.4, 5.0。

I tried in many ways, like popupWindo.setFocus(true), which only make the EditText lose the focus.

So, Can android allow two views both have focus ?


回答1:


No, two (or more) views cannot have focus at the same time, not possible.

Here is how to change focus from one textView to another textView. Which you could change in your case.




回答2:


Actually, technically speaking two views can have focus at the same time - not just as you would imagine (and as you would need for your usecase):

The parents of a focused view can also have their focus set to true. Cf. an excerpt from an example view hierarchy, has-focus=true pops out more than once:

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1920, height=1080, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, 
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1776, height=1080, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=fal
|
+-->ViewStub{id=16909243, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-f
|
+-->FrameLayout{id=-1, visibility=VISIBLE, width=1776, height=1008, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=fal
|
+--->FitWindowsLinearLayout{id=2131296268, res-name=action_bar_root, visibility=VISIBLE, width=1776, height=1008, has-focus=true, has-focusable
|
+---->ViewStubCompat{id=2131296279, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, ha
|
+---->ContentFrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1776, height=1008, has-focus=true, has-focusable=true, has-wi
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=1776, height=1008, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable
|
+------>RecyclerView{id=2131296483, res-name=lstRecordings, visibility=VISIBLE, width=1776, height=873, has-focus=true, has-focusable=true, has


来源:https://stackoverflow.com/questions/27289381/can-two-views-both-have-focus-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!