从主布局访问所包含布局的所有子元素

时间:2016-12-15 17:56:28

标签: android xml android-layout android-include

我有activity_main布局,看起来像以下布局:

<RelativeLayout
    android:id = "@+id/wrapper"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_alignParentBottom = "true"
    android:layout_gravity = "bottom"
    android:gravity = "bottom">
    <include
        android:id = "@+id/call_to_action"
        layout = "@layout/call_to_action_layout"
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"/>
    <include
        android:id = "@+id/status"
        layout = "@layout/status_layout"
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"/>
</RelativeLayout>

我所包含的两个布局都是这样的:

这是call_to_action_layout.xml

<LinearLayout
    android:id = "@+id/action_button_container"
    android:layout_height = "wrap_content"
    android:layout_width = "fill_parent"
    xmlns:android = "http://schemas.android.com/apk/res/android">

    <EditText
        android:id="@+id/edit_text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <EditText
        android:id="@+id/edit_text_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

status_layout.xml

<LinearLayout
    android:id = "@+id/another_container"
    android:layout_height = "wrap_content"
    android:layout_width = "fill_parent"
    xmlns:android = "http://schemas.android.com/apk/res/android">

    <EditText
        android:id="@+id/edit_text_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <EditText
        android:id="@+id/edit_text_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

MainActivity.java我正在尝试设置EditText中存在的所有activity_main.xml的属性。为此,我需要遍历并访问布局中存在的每个EditText

所以这就是我迄今为止在MainActivity.java

中尝试过的内容
RelativeLayout container = findViewById(R.id.wrapper);

for (int i = 0; i < container.getChildCount(); i++) {
    Log.d(TAG, "id: " + container.getChildAt(i));

    if (container.getChildAt(i) instanceof EditText) {
        EditText editText =
            (EditText) container.getChildAt(i);
        editText.setKeyListener(null);
    }
}

但由于<include>代码,我无法访问此代码中的所有EditText

有没有办法做到这一点?

注意:基本上,所有想要做的就是禁用布局中存在的所有EditText

1 个答案:

答案 0 :(得分:1)

在你的循环中,你只是在遍历布局的第一级孩子(call_to_actionstatus)。

你可以这样做两个循环:

ViewGroup call_to_action = findViewById(R.id.call_to_action)
for (int i = 0; i < call_to_action.getChildCount(); i++)
{
    Log.d(TAG, "id: " + call_to_action.getChildAt(i));
    if (container.getChildAt(i) instanceof EditText)
    {
        EditText editText =
            (EditText) call_to_action.getChildAt(i);
        editText.setKeyListener(null);
    }
}

ViewGroup status = findViewById(R.id.status)
for (int i = 0; i < status.getChildCount(); i++)
{
    Log.d(TAG, "id: " + status.getChildAt(i));
    if (status .getChildAt(i) instanceof EditText)
    {
        EditText editText =
            (EditText) status.getChildAt(i);
        editText.setKeyListener(null);
    }
}

或者创建一个像这样的递归方法:

public void removeKeyListeners(ViewGroup container){
for (int i = 0; i < container.getChildCount(); i++)
    {
        Log.d(TAG, "id: " + container.getChildAt(i));
        if (container.getChildAt(i) instanceof EditText)
        {
            EditText editText =
                (EditText) container.getChildAt(i);
            editText.setKeyListener(null);
        } else if (container.getChildAt(i) instanceof ViewGroup) {
              removeKeyListeners((ViewGroup) container.getChildAt(i));
        }
    }
}

并将其调用为您的布局:

removeKeyListeners(container);

注意:此处使用ViewGroup因为所有Layouts都是它的子类。