So habe ich zur Zeit einen Android-App, die mit dem Knopfdruck eine Zeile eines linearen Layout hinzuzufügt und entfernt. Das lineare Layout enthält zwei Bearbeitungstexte und zwei Textansichten, aber die Zeile wird am Ende der Liste hinzugefügt, und ich frage mich, ob es eine Möglichkeit gibt, eine Zeile des linearen Layouts hinzuzufügen/zu entfernen, die vom ausgewählten Bearbeitungstext abhängt.Wie dynamisch hinzufügen/entfernen lineares Layout in Android Studio
Beispiel: Ich habe 4 Zeilen von linearen Layouts und ein EditText in der 2. Zeile ausgewählt ist, I die Taste hinzufügen und dann wird eine Linie zwischen den 2. und 3. Zeile eingefügt. Unten ist mein Code
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;
}