有人可以解释后裔焦点=后裔吗? [英] Can someone explain descendantFocusability = afterDescendants?

查看:106
本文介绍了有人可以解释后裔焦点=后裔吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having hard time understanding descendantFocusability. Very particulary afterDescendants.

有人可以告诉我一个什么时候有用的例子吗?

Could someone show me an example of when this would be useful?

推荐答案

定义ViewGroup及其后代之间的关系,以寻找View作为焦点.

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

必须是以下常量值之一.

Must be one of the following constant values.


+------------------------------------------------------------------------------------------+
|      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.

代码段是:

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都可以请求焦点.

As per the above snippet, afterDescendants is used to prevent listview from getting focus, so that either EditText or Button can request focus.

注意:上面提供的链接已断开.请参阅我的要点以获取代码

Note: The link provided above is broken. Please refer to my Gist for the code

这篇关于有人可以解释后裔焦点=后裔吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆