2012-06-21 28 views
1

我試圖使用兩個按鈕來動態添加和刪除線性佈局。如何通過按鈕單擊添加和刪除線性佈局

protected void createT() { 
    // ----------------------------------------------- 
    count++; 
    LinearLayout temp_ll, frame; 
    frame = new LinearLayout(this); 
    frame.setOrientation(LinearLayout.VERTICAL); 
    frame.setId(count); 
    EditText temp1, temp2; 

    for (int i=0; i<numClass; i++) { 

     temp_ll = new LinearLayout(this); 
     temp_ll.setOrientation(LinearLayout.HORIZONTAL); 

     temp1 = new EditText(this); 
     temp2 = new EditText(this); 
     temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 
     temp1.setHint("class name"); 
     temp2.setHint("grade"); 

     temp_ll.addView(temp1); 
     temp_ll.addView(temp2); 
     frame.addView(temp_ll); 
    } 

    ll.addView(frame); 
} 

protected void deleteT() { 
    // -------------------------------------- 
    if (count > 0) { 
        LinearLayout temp = new LinearLyout(this); 
     temp = (LinearLayout) findViewById(count); 
     temp.removeAllViews(); 
     temp.setVisibility(View.GONE); 
     count--; 
    } 
} 
  • 我每次按添加按鈕,它會調用createT()
  • 每次我按下德爾按鈕,它會調用deleteT()

的問題是,我使用計數變量來跟蹤linearLayout ID。
第一次當我按按鈕x次,然後按del按鈕一切都很好。

問題在於壓制後ADD第二次。

+0

什麼是錯誤日誌? –

+0

沒有錯誤,只是「del」按鈕在第一次沒有響應後 – Brian

+0

想要刪除LinearLayout或任何特定的一個...形式向上或向下? –

回答

4

我檢查了你的代碼,發現deleteT()方法裏面有一個小錯誤,你刪除了LinearLayout中的所有視圖,但是你沒有從根佈局中刪除實際的佈局。

另一個建議是,當您想使用findviewbyid方法時,不需要新的LinearLayout實例,並使用ll.removeView(View)方法刪除動態LinearLayout容器。

下面是代碼:

private int count; 
private LinearLayout ll; 
private final int numClass = 1; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ll = (LinearLayout) findViewById(R.id.main_linearlayout); 
} 

public void createT(View view) { 
    count++; 
    final LinearLayout frame = new LinearLayout(this); 
    frame.setOrientation(LinearLayout.VERTICAL); 
    frame.setId(count); 

    for (int i = 0; i < numClass; i++) { 
     final LinearLayout temp_ll = new LinearLayout(this); 

     final EditText temp1; 
     final EditText temp2; 
     temp_ll.setOrientation(LinearLayout.HORIZONTAL); 

     temp1 = new EditText(this); 
     temp2 = new EditText(this); 
     temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 
     temp1.setHint("class name"); 
     temp2.setHint("grade"); 

     temp_ll.addView(temp1); 
     temp_ll.addView(temp2); 
     frame.addView(temp_ll); 
    } 
    ll.addView(frame); 

} 

public void deleteT(View view) { 
    if (count > 0) { 
     final LinearLayout temp = (LinearLayout) ll.findViewById(count); 
     temp.removeAllViews(); 
     ll.removeView(temp); 
     count--; 
    } 
} 

而且佈局XML:

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

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="add" 
     android:onClick="createT" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="delete" 
     android:onClick="deleteT" /> 

</LinearLayout> 

我希望它能幫助!

+0

如果你檢查我的代碼上面有一行: {temp.setVisibility(View.GONE); } ---這將刪除根佈局。 我有沒有你提到的那一行代碼,它沒有工作 – Brian

+0

你不能刪除View.GONE的視圖佈局仍然在父視圖下,只有不可見(從父視圖隱藏)並且該ID正在使用中。我用你的代碼創建了一個示例應用程序,它使用removeView()方法進行perfetly。有關更多信息,請閱讀[查看參考](http://developer.android.com/reference/android/view/View.html)並檢查setVisibility()方法和參數VISIBLE,GONE和INVIIBLE。 – kameny

+0

我更新了我的答案,以便您可以檢查類和佈局xml。 – kameny

相關問題