gpt4 book ai didi

android - 有人可以解释 descendantFocusability = afterDescendants 吗?

转载 作者:IT老高 更新时间:2023-10-28 21:37:24 31 4
gpt4 key购买 nike

我很难理解 descendantFocusability。非常特别afterDescendants

有人可以给我举个例子说明什么时候有用吗?

最佳答案

在寻找 View 以获取焦点时,定义 ViewGroup 与其后代之间的关系。

必须是以下常量值之一。

+------------------------------------------------------------------------------------------+|      Constant            Value            Description                                    |+------------------------------------------------------------------------------------------+| afterDescendants           1          The ViewGroup will get focus only if               ||                                       none of its descendants want it.                   |+------------------------------------------------------------------------------------------+| beforeDescendants          0          The ViewGroup will get focus before                ||                                       any of its descendants.                            |+------------------------------------------------------------------------------------------+| blocksDescendants          2          The ViewGroup will block its descendants from      ||                                       receiving focus.                                   |+------------------------------------------------------------------------------------------+

You can check the complete example here.

The snippet is :

public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
ListView listView = getListView();
Log.d(TAG, "onItemSelected gave us " + view.toString());
Button b = (Button) view.findViewById(R.id.button);
EditText et = (EditText) view.findViewById(R.id.editor);
if (b != null || et != null) {
// Use afterDescendants to keep ListView from getting focus
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
if(et!=null) et.requestFocus();
else if(b!=null) b.requestFocus();
} else {
if (!listView.isFocused()) {
// Use beforeDescendants so that previous selections don't re-take focus
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
listView.requestFocus();
}
}

}

根据上述代码段,afterDescendants 用于防止 listview 获得焦点,因此 EditTextButton 可以请求焦点。

注意:上面提供的链接已损坏。请引用我的Gist代码

关于android - 有人可以解释 descendantFocusability = afterDescendants 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39626337/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com