创建多个视图

时间:2015-04-28 23:23:36

标签: android view textview

我们假设我有一个ScrollView和一个Actionbar按钮,可以将视图添加到ScrollView。这个View包含一个简单的EditText。用户可以根据需要添加尽可能多的Views,并为每个View分配一个唯一的号码。我能够访问每个View并阅读其编号吗?

// View to add
addView() {
   newView = (ViewGroup) ...inflate(R...);
   text = (EditText) findViewById();
   scrollView.addView(newView); }

用户添加了多个Views并为每个TextView分配了一个号码后,我想读取这些号码并将其存储在ArrayList<String>中。 我不知道如何处理这个问题。

1 个答案:

答案 0 :(得分:0)

循环播放

ArrayList<String> strings = new ArrayList<String>();
ViewGroup vg = scrollView.getChildAt(0); // get its direct child
for(int i = 0; i < vg.getChildCount(); i++){
  //we loop through the views in the scrollview's direct 
  //child and get the views and do what we want to do
  strings.add(((EditText)vg.getChildAt(i)).getText().toString());
}