2013-04-12 1 views
0

저는 Android 개발의 초보자이며이 점에 대해 도움을 주시면 감사하겠습니다.Android - 활동이 시작된 후에 추가 된 개체에 액세스하십시오.

사용자가 버튼을 클릭 할 때 Checkbox 및 EditText를 만드는 Android 액티비티가 있습니다. 사용자는 그가 원하는만큼 많은 체크 박스/편집 텍스트를 자유롭게 추가 할 수 있습니다.

public void onclickplus (View view){ 

     LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View view1 = inflate.inflate(R.layout.row_checkitem, null); 
     mainLayout.addView(view1); 
     count = count +1; 

} 

내 질문은, 우리가 어떻게 액세스합니까된다

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 

    </LinearLayout> 

</LinearLayout> 

이것은 자바 코드는 다음과 같습니다

은 활동에 추가되는 뷰의 XML 파일입니다 이 체크 박스와 EditTexts가 실제 자바 코드에 추가되어 데이터를 데이터베이스에 저장할 수 있습니다.

내가 예를 들어 할 것 미리 정해진 이러한 개체 경우 등

EditText text1; 
text1 = (EditText) findViewById(R.id.editText1); 
text1.getText().toString(); 

하고, EDITTEXT의 값에 액세스 할 수 있습니다.

그러나이 경우 활동이 시작될 때 이러한 개체가 없습니다.

나를 도와 줄 수 있습니까? 감사합니다. .

답변

0

이 솔루션을 볼 수 있었다 :

Add EditText(s) dynamically and retrieve values – Android

그들이 동적으로 다수의 EditText 필드를 추가하고 목록에 저장되어 할 것은 나중에에 액세스 할 수 있습니다. 체크 박스를 추가하기 만하면됩니다.

+0

안녕하십니까. 답변 해 주셔서 감사합니다. 링크 된 솔루션을 살펴본 결과, 다음 코드를 사용하여 많은 양의 EditText를 만들고 해당 값에 액세스 할 수 있습니다. EditText [] ed; ed = 새 EditText [count]; \t \t \t \t 찾는 INT (I = 0; I ++가 나는 카운트 <) { \t \t \t ED [I] = 새로운 글고 (이); \t \t \t ed [i].setId (i); \t \t \t \t \t \t mainLayout.addView (ed [i]); \t \t} 그러나 이해할 수 있듯이 사용자가 XML보기를 부풀리고 추가하거나 제거하는 것과 달리 사용자가 선택한 양만 추가 할 수 있습니다. 여전히 이러한 체크 박스에 액세스하는 방법을 찾고 있으며 XML보기를 부 풀려서 추가 한 editTexts가 있습니다. –

+0

왜 그걸 원하니? 이 예제를 보면 레이아웃에 'tableLayout.addView (createOneFullRow (rowId));'를 추가하기 만하면됩니다. 'ed [i] .getText(). toString(); '을 사용하여 EditText 필드에 액세스 (예 : 텍스트 가져 오기) 할 수 있습니다. 필드를 동적으로 추가하고 액세스합니다. –

+0

다시 연락 드리겠습니다. 오늘 밤에 작업하고 어떻게 작동하는지보십시오. 감사합니다. . –

0

저는 D.Wonnik의 도움 덕분에 내가하고 싶은 것을 성취 할 수있었습니다.

나는 LayoutInflater 메서드를 포기하고 대신 TableLayout과 TableRows를 사용했습니다.

사용자는 "+"버튼을 클릭하여 체크 박스와 EditText가 포함 된 TableRow를 추가 할 수 있으며 "-"버튼을 클릭하여 적합하다고 판단한 경우이를 제거 할 수도 있습니다. 나는 또한이 방법으로 값을 검색 할 수있다.

LinearLayout mainLayout; 
    TableLayout tablelayout; 
    TableRow[] tableRow; 
    int count; 
    EditText[] ed; 
    CheckBox[] cb; 

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     mainLayout = (LinearLayout)findViewById(R.id.LinearLayout1); 
     tablelayout = new TableLayout(this); 
     mainLayout.addView(tablelayout); 
     ed = new EditText[10]; 
     cb = new CheckBox[10]; 
     tableRow = new TableRow[10]; 

    } 

public void onclickplus (View view){ 


     ed[count] = new EditText(this); 
     ed[count].setId(count); 
     cb[count] = new CheckBox(this); 
     cb[count].setId(count); 
     tableRow[count] = new TableRow(this); 

     tablelayout.addView(tableRow[count]); 

     tableRow[count].addView(cb[count]); 
     tableRow[count].addView(ed[count]); 

     count = count +1; 

} 

public void onclickminus (View view){ 


     if (count>0){ 

      count = count -1; 
      tablelayout.removeView(tableRow[count]); 
    } 

    } 

가 올바른 방향으로 날을 가리키는 당신에게 D.Wonnik 감사합니다

여기에 코드입니다!