2017-01-09 10 views
0

現在、ボタンを押して線形レイアウトの線を追加して削除するアンドロイドアプリがあります。 Linearレイアウトには2つのedittextと2つのtextviewが含まれていますが、リストの最後に行が追加されています。選択されたedittextに依存する線形レイアウトの行を追加/削除する方法があるのでしょうか?アンドロイドスタジオの線形レイアウトを動的に追加/削除する方法

例:4行の線形レイアウトがあり、2行目の編集テキストが選択されたら、追加ボタンを押して2行目と3行目の間に行を挿入します。 以下はおそらく、あなたは、ArrayListのを構築することができますし、最初にそれを修正する私のコード

ToggleButton timeTempToggle; 
ArrayList<EditText> dataET = new ArrayList<>(); 

Button buttonAddStep,buttonRemoveStep; 

int id=1; //the id number which indicates the number of commands that will be used 


LinearLayout mainLayout; //orientation is vertical 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.new_parameters); 
    buttonAddStep = (Button) findViewById(R.id.buttonAddStep); 
    buttonRemoveStep = (Button) findViewById(R.id.buttonRemoveStep); 
    mainLayout = (LinearLayout) findViewById(R.id.mainlayout); 




    buttonAddStep.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(id<8) { 
       //adds a stage to be included (limit is 8) 
       id++; 
       mainLayout.addView(linearlayout(id,"","0")); 
       //append after all lines 
      } 

     } 
    }); 

buttonRemoveStep.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(id>1){ // removes a step from the list of steps 
       //removes the stage from the thermal cycling procedure 
       //will not remove the first step 
       id--; 
       mainLayout.removeViewAt(id); 
       dataET.remove(dataET.size()-1); 
       dataET.remove(dataET.size()-1); 
       //remove the last value and line 
      } 
     } 
    }); 
} 
private LinearLayout linearlayout(int _intID, String et1, String et2) 
{ 
    LinearLayout LLMain=new LinearLayout(this); 
    LLMain.setId(_intID); 
     LLMain.addView(textView(_intID, String.valueOf(_intID) + " Temp (C): ")); 
     LLMain.addView(editText(_intID, et1)); 
     LLMain.addView(textView(_intID + 1, "Time(s): ")); 
     LLMain.addView(editText(_intID + 1, et2)); 


    LLMain.setOrientation(LinearLayout.HORIZONTAL); 
    return LLMain; 
} 

private EditText editText(int _intID,String preset) { 
    EditText editText = new EditText(this); 
    editText.setId(_intID); 
    editText.setInputType(InputType.TYPE_CLASS_NUMBER); 
    editText.setWidth(130); 
    editText.setText(preset); 
    dataET.add(editText); 
    return editText; 
} 


private TextView textView(int _intID,String Type) 
{ 
    TextView txtviewAll=new TextView(this); 
    txtviewAll.setId(_intID); 
    txtviewAll.setText(Type); 
    txtviewAll.setTypeface(Typeface.DEFAULT_BOLD); 
    return txtviewAll; 
} 

答えて

0

です。リスト内の一連のものを管理することができます。 ArrayListが好きな順番になったら、for-eachループでそれを必要な場所に追加します。

EDIT。 1)これは、ビューを再作成する必要があることを意味します。 2)最大数が8であるため、それほど悪くはありません。数字がNならば、私はこの簡単な解決策をお勧めしません。

0

void setVisibility (int visibility)を使用してビューの使用Visibility

もっと

here

enter image description here

関連する問題