如果我在单击按钮时在同一父级中多次膨胀视图,我如何知道视图的 ID?
我遇到了一个困难的情况,让我这样说,我希望用户使用我的 3 个文本字段来设置警报数据,填写信息后,我给他一个选项来设置另一个警报,我使用一个按钮。我在表布局中有三个表行,它们在 child.xml 文件中定义,并且每个元素都有一个 Id 集。在我的活动中,我在按钮单击事件上多次膨胀 child.xml,并且我能够根据需要成功多次膨胀它。但我的问题是,因为我在运行时多次(我不知道多少次)膨胀同一个 xml 文件,所以我很困惑如何从特定文本字段(如 TextViews)检索数据并通过唯一的 ID,因为我需要这样做。 你怎样做呢?
I have a difficult situation, let me put it this way, I want the user to use my 3 text-fields to set data for alarm and after filling in the information, I give him an option to set another alarm for this purpose I use a button. I have three table rows in a table layout which are defined in a child.xml file and every element has an Id set. In my activity, I inflate child.xml multiple times on button click event and I am able to successfully inflate it as many times as I want. But my problem is since I am inflating the same xml file many times(I don't know how many times) at runtime, I am confused as of how would I retrieve the data from particular text-fields like TextViews and identify them by a unique IDs since I am required to do so.
How do you go about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会以编程方式解决这个问题。
当用户请求新警报时,您会膨胀 xml 文件。此时,在您的活动中保留对膨胀视图的引用(在列表或某种其他类型的集合中)。
因此,当您想要检索数据时,不要使用
findViewById
,而只需使用您的引用即可。I would go programmatically about it.
When the user requests for a new alarm, then you inflate your xml file. At this point, keep a reference to the inflated view (in a List, or some other sort of collection) in your activity.
So when you want to retrieve the data, don't use
findViewById
, just use your reference instead.也许这可以帮助...
我的 addit.xml 文件包含以下内容:
这里我从 addit.xml 动态添加布局 3 次,其中包含 id t1 的文本视图..
对于每个文本视图,我从字符串数组 str ..
并将每个视图对象 v 添加到数组中以供引用......
现在,如果我想获取每个文本视图的文本,我可以使用对存储在数组中的视图的引用,并按照代码中的 id 查找文本视图,以从每个文本视图获取文本
may be this can help...
my addit.xml file contains the following :
here i m adding dynamically the layout from addit.xml 3 times which contains the textview with id t1..
for each textview i m setting a text from the string array str ..
and adding each view object v to an array for referencing ...
now if i want to get the text for each textview i can use the reference to the view stored in the array and find the text view by id as in the code to get the text from each textview
我将从基本的
现在开始活动
现在假设您已经膨胀了另一个视图,但 view1 值没有更改,那么 btn 将包含该按钮的引用。当它变得可见时。它将像普通按钮一样工作。
i will start from basic
Now in activity
Now suppose you have inflate another view but view1 value is not changed then btn willl contain reference for that button.And when it became visible.It will work as like normal button.
只需记住在每个实例
inflate()
时返回的View
引用即可。当您需要从中读取文本时,将它们投射到TextView
上。Just remember the
View
reference you get back when youinflate()
each instance. Cast these toTextView
when you need to read text from them.