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

标签 java android android-layout

我正在开发一个允许用户填写表单的 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/

相关文章:

android - 简单的 Activity 开关不起作用。没有错误

android xml标签字符串组合

android - 如何实现100毫秒休眠的重试机制

java - ANDROID - 如何保存 View 中包含的内容并在应用程序重新启动时重新创建它

android - 奇怪的重绘?在 android 上的 webview 中发布 html 应用程序

Android 通过 HTML 上的媒体查询忽略字体大小更改

Java扫描仪问题

java - 什么是 p5.js textToPoints() 字体函数的等效处理?

java - iText 7 - 将 PDF 图层 (OCG) 与基础 PDF 合并

java - 更改 Arr 文件然后将其包含在 Android Studio 中