簡體   English   中英

從editText獲取內容

[英]Getting content from editText

在我的應用程序中,我有一個tableLayout里面有很多editTexts。 當我單擊“保存”按鈕時,我想訪問在editTexts中輸入的所有值。 我在創建表時在運行時分配了ID。 現在,當單擊“保存”按鈕時,如何從editTexts中訪問值? 我已經使用了以下類似的方法來分配ID,

for(int i=0;i< no_of_rows ;i++)
  for(int j=0;j<5;j++)
  {   
    ...............
    assignment.setId(i+j);
    .............
  }

有人可以建議解決方案嗎?

怎么樣:

ArrayList<String> strings = new ArrayList<String>();
for(int i=0;i< no_of_rows ;i++)
  for(int j=0;j<5;j++)
  {   
    EditText text = (EditText)ActivityName.this.findViewById(i+j);
    strings.add(text.getText().toString());
  }
}

這樣一來,您就可以將所有文本中的所有值都放在一個大數組中。 如果那不是您想要的,請告訴我,我將看看是否可以調整代碼。

另一個不錯的解決方案是遍歷某個Layout的所有子項,因此您無需設置任何特殊的ID。 試一試:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
child_count = layout.getChildCount();

for (int i=0; i<child_count; i++)
{
    EditText text = (EditText) layout.getChildAt(i);
    // do work with text
}

使用其他代碼,您可以對任何其他布局層次結構執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM