마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

동적layout의 gettext를 어떻게 해야하나요?

0 추천

버튼을 두고 동적으로 layout을 추가하고 삭제하려 합니다.

동적으로 추가된 edittext의 string을 가져오려 하는데 방법을 모르겠습니다.

public class MainActivity extends Activity {

 private LinearLayout mContainerView;
 // The "Add new" button
 private Button mAddButton;

 // There always should be only one empty row, other empty rows will
 // be removed.
 private View mExclusiveEmptyView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.row_container);

  mContainerView = (LinearLayout) findViewById(R.id.parentView);
  mAddButton = (Button) findViewById(R.id.btnAddNewItem);

  // Add some examples
  inflateEditRow("Xiaochao");
  inflateEditRow("Yang");
 }

 // onClick handler for the "Add new" button;
 public void onAddNewClicked(View v) {
  // Inflate a new row and hide the button self.
  inflateEditRow(null);
  v.setVisibility(View.GONE);
 }

 // onClick handler for the "X" button of each row
 public void onDeleteClicked(View v) {
  // remove the row by calling the getParent on button
  mContainerView.removeView((View) v.getParent());
 }

 // Helper for inflating a row
 private void inflateEditRow(String name) {

  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  final View rowView = inflater.inflate(R.layout.row, null);
  final ImageButton deleteButton = (ImageButton) rowView
    .findViewById(R.id.buttonDelete);
  final EditText editText = (EditText) rowView
    .findViewById(R.id.editText);

  if (name != null && !name.isEmpty()) {
   editText.setText(name);
  } else {
   mExclusiveEmptyView = rowView;
   deleteButton.setVisibility(View.INVISIBLE);
  }

  // A TextWatcher to control the visibility of the "Add new" button and
  // handle the exclusive empty view.
  editText.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {

    if (s.toString().isEmpty()) {
     mAddButton.setVisibility(View.GONE);
     deleteButton.setVisibility(View.INVISIBLE);

     if (mExclusiveEmptyView != null
       && mExclusiveEmptyView != rowView) {
      mContainerView.removeView(mExclusiveEmptyView);
     }
     mExclusiveEmptyView = rowView;
    } else {

     if (mExclusiveEmptyView == rowView) {
      mExclusiveEmptyView = null;
     }

     mAddButton.setVisibility(View.VISIBLE);
     deleteButton.setVisibility(View.VISIBLE);
    }
   }

   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
     int after) {
   }

   @Override
   public void onTextChanged(CharSequence s, int start, int before,
     int count) {
   }
  });

  // Inflate at the end of all rows but before the "Add new" button
  mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
 }
}

위와 같은 상황에서 맨아래 버튼을 하나 두어서 클릭시 위에 있는 edittext에 있는 모든 text를 들고오고 싶은데 방법을 모르겠습니다.

inflateEditRow 메소드 부분에서 setid와 settag를 이용하여 설정을 해주고 가져오고 싶은데 이것도 쉽게 안되네요...

고수님들 도움 요청드립니다.

 

wind (2,240 포인트) 님이 2013년 4월 1일 질문
Tag 를 순서대로 주시면 됩니다..0 ,1,2  나중에 geTag()로 가지고 오면 index 구해올 수 잇을 것 같습니다.. Tag 는 object 상속이니까 편하게 쓰실 수 있을듯요..

1개의 답변

0 추천
 
채택된 답변
단순 text만 가지고 오고 싶으신거라면 edittext 를 생성할때, ArrayList<EditText> 를 이용하셔서

배열에 집어 넣고 나중에 for문 돌려서 값을 가져오면 될듯 싶은데요.

 

그런게 아니라면 layout에 뷰를 추가할때마다 순차적으로 인덱스가 하나씩 부여되는데,

editText가 몇번째에 추가됬는지 확인하셔서, getChildAt(인덱스) 로 꺼내오시는 방법도 있습니다만..

 

후자보다는 전자쪽이 간편해 보이긴 합니다 ~_~;
Frog (3,910 포인트) 님이 2013년 4월 1일 답변
wind님이 2013년 4월 2일 채택됨
...