gpt4 book ai didi

android - 单体机器人 : Iterate over the controls in an Activity?

转载 作者:行者123 更新时间:2023-11-29 18:14:48 24 4
gpt4 key购买 nike

在 Activity.OnContentChanged 中,我想遍历所有控件以获取它们的标签。

我认为它与 base.Window.HasChildren 有关,但我无法弄清楚如何实际获取子项。

编辑:ViewGroup 是大多数布局的基类,具有 GetChildCount 和 GetChildAt。但我仍然无法弄清楚如何从 Activity 到根级布局。

最佳答案

您可以为布局的根元素分配一个 ID 并通过它访问它:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="2"/>
</LinearLayout>

然后在你的 Activity 中你可以做这样的事情:

var root = FindViewById<LinearLayout>(Resource.Id.Root);

for (int i = 0; i < root.ChildCount; i++)
{
Console.WriteLine(root.GetChildAt(i).Id);
}

关于android - 单体机器人 : Iterate over the controls in an Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8800235/

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