gpt4 book ai didi

java - 有没有办法检索以动态方式创建的控件(线性布局)的信息?

转载 作者:行者123 更新时间:2023-12-01 18:52:30 24 4
gpt4 key购买 nike

我正在开发一个允许用户填写表单的 Android 项目。

下图描述了我的表单的不同布局。 This is my form

黑色布局(LinearMain)是我主要的线性布局。绿色的(LinearForm)描述了表单的行。

LinearMain.addView(LinearForm);

蓝色的(LinearDescription)包含要填写的表单行的描述。

LinearForm.addView(LinearDescription);

灰色的线性布局描述了我的控件。该控件可以是 EditText、按钮或 CheckBox。

For example LinearForm.addView(LinearButton)

填写表单后,我想检索 EditText 中的文本,了解复选框是否已选中。我已经尝试使用 ArrayList(示例 ArrayList),但它不是很有帮助。

还有其他方法可以解决这个问题吗?

最佳答案

因为您不确切知道要处理的 View ,所以在这里使用实例来避免异常很重要

ViewGroup是一个抽象类,它扩展了所有ViewGroup,例如LinearLayout就是一个ViewGroup。

 if(ViewGroup.getChildAt(int) instanceof Checkbox){

//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof Button){

//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof TextView){

//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof EditText){

//do sommethng here
}

关于java - 有没有办法检索以动态方式创建的控件(线性布局)的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15337094/

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