在使用ViewBinding而不是Kotlin合成材料时,如何设置视图ID?

发布于 2025-01-20 16:55:19 字数 1043 浏览 0 评论 0原文

我正在更新一个 Android 项目以使用 ViewBinding 而不是 Kotlin Synthetics。我很难弄清楚如何更改以下代码,以便我可以从布局 ID 访问视图。

binding.myLinearLayout.children
            .filter { it.checkboxInput is CheckBox }

在这种情况下,children 都是通用的 View 类型,无法像以前使用 Kotlin Synthetics 那样访问 checkboxInput ID

我收到错误未解析的参考:checkboxInput

解决此问题的方法是什么?有没有办法检查视图是否是绑定类型?我需要创建自定义视图类来执行此操作吗?还有其他想法吗?

感谢您的帮助!

编辑

我还有另一个有点令人困惑的案例。

binding.formItems.children
        .filter { it.getTag(R.id.tag_guest_identifier) != null }
        .map { view ->
            Guest(
                guestIdentifier = view.getTag(R.id.tag_guest_identifier).toString(),
                name = view.playerName.valueText.toString(),
                ...
            )
        }
}

在这里,我得到了通用View的列表,因此我无法访问它们的属性(即view.playerName...等)。

我是否需要创建一个 View 子类,然后将视图转换为该类型?有没有更简单的方法来实现这一目标?

再次感谢!

I am updating an Android project to use ViewBinding instead of Kotlin Synthetics. I'm having difficulty figuring out how to change the following code so I can access the views from their layout IDs.

binding.myLinearLayout.children
            .filter { it.checkboxInput is CheckBox }

In this case children are all generic View types and can't access the checkboxInput IDs like it used to be possible using Kotlin Synthetics.

I get the error Unresolved reference: checkboxInput

What would be the way to solve this? Is there a way to check if the View is of a binding type? Do I need to make custom View classes to do this? Any other ideas?

Thanks for your help!

EDIT

I have another case that's a bit confusing.

binding.formItems.children
        .filter { it.getTag(R.id.tag_guest_identifier) != null }
        .map { view ->
            Guest(
                guestIdentifier = view.getTag(R.id.tag_guest_identifier).toString(),
                name = view.playerName.valueText.toString(),
                ...
            )
        }
}

Here, I get a list of generic Views so I can't access their properties (ie. view.playerName... etc.).

Do I need to create a View subclass and then cast the view to that type? Is there an easier way to achieve this?

Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相守太难 2025-01-27 16:55:19

查看绑定基本上与合成材料相同的方法,除非您拥有具有condbox_input的ID的东西,而不是在活动activechackboxinput contive> activity active 。 /code>或erther,它会在viewBinding对象中创建它。因此,如果您以前这样访问它:

// not declared anywhere, it's just magically there for you to use
checkboxInput

现在,您可以在viewBinding对象上访问它:

binding.checkboxInput

您不需要进行任何搜索,这会破坏观点绑定!它是自动绑定的视图,可与方便对象中的变量进行自动绑定。

您的代码将与过滤器一起使用{是复选框},然后您将在布局的那部分中获取所有复选框项目(您也可以使用> filterisinstance< checkbox>,同一件事)。但是,如果您想要具有特定ID的一个,则必须查看其ID属性 - 到那时,也可能只需使用findViewById

View binding works basically the same way synthetics did, except when you have something with an ID of checkbox_input, instead of magically creating a variable called checkboxInput on the Activity or whatever, it creates it in the ViewBinding object instead. So if you were accessing it like this before:

// not declared anywhere, it's just magically there for you to use
checkboxInput

now you access it on the ViewBinding object instead:

binding.checkboxInput

You don't need to do any searching, that defeats the point of view binding! It's automagically binding views to variables in a convenient object.

Your code would work with filter { it is CheckBox }, and then you'd get all the Checkbox items within that part of the layout (you can also use filterIsInstance<CheckBox>, same thing). But if you wanted the one with a specific ID, you'd have to look at its ID attribute - and at that point, might as well just use findViewById!

木落 2025-01-27 16:55:19

使用ViewBinding,您可以访问视图键入仅

绑定。

查看viewID是XML中每个视图的ID,使用attribuite

android:ID

访问复选框。

在您的情况下,您可以使用binding.checkboxinput

With viewBinding you can access views typing just

binding.viewId

Where viewId is the id defined for each view in your xml with the attribuite

android:id

In your case you can access the checkbox using

binding.checkboxInput

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文