我正在开发一个允许用户填写表单的 Android 项目。
下图描述了我的表单的不同布局。
黑色布局(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/