Je veux ajouter une vue EditText "ingrédient" à côté d'une vue EditText "quantité", en appuyant sur un bouton "ajouter". Voici le code de mise en page de départ:
<LinearLayout android:id="@+id/ingredients_line_layout"
android:layout_below="@+id/title_line_layout"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_margin"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditText
android:id="@+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:hint="@string/ingredients"
android:inputType="text" />
<EditText
android:hint="@string/quantity"
android:id="@+id/quantityField"
android:inputType="number"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content" />
<com.google.android.material.button.MaterialButton
android:id="@+id/add_ingredient_btn"
android:layout_height="wrap_content"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content"
android:text="@string/add"
app:icon="@drawable/ic_add_ingr_btn" />
</LinearLayout>
Comment puis-je implémenter la méthode onClick pour le bouton, dans l'activité correspondante?
3 Réponses :
Étape 1: écrivez toutes les vues dans le fichier XML et masquez "ingrédient" EditText
@Override
public void onClick(View v) {
ingredientsField.setVisibility(View.VISIBLE);
}
Étape 2: Inscrivez-vous en cliquant sur auditeur sur le bouton d'ajout.
addIngredientBtn.setOnClickListener(this);
Étape 3: "ingrédient" visible sur le bouton cliquez sur
<LinearLayout android:id="@+id/ingredients_line_layout"
android:layout_below="@+id/title_line_layout"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_margin"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditText
android:hint="@string/quantity"
android:id="@+id/quantityField"
android:inputType="number"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content" />
<EditText
android:id="@+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="@dimen/boxes_margin"
android:hint="@string/ingredients"
android:visibility="gone"
android:inputType="text" />
<com.google.android.material.button.MaterialButton
android:id="@+id/add_ingredient_btn"
android:layout_height="wrap_content"
android:layout_margin="@dimen/boxes_margin"
android:layout_width="wrap_content"
android:text="@string/add"
app:icon="@drawable/ic_add_ingr_btn" />
</LinearLayout>
De cette façon, vous pouvez ajouter Dynamiquement EditText.
EditText etIngredient = new EditText(context); // pass Context here etIngredient.setLayoutParams(new LayoutParams(..., ...)); // two Arguments such as LayoutParams.WRAP_CONTENT layout.addView(etIngredient); You might wanna add weights to the layout holding the editText accordingly if there are two edit text beside each other.
OnClickListener pour le bouton "ajouter"; ingredients_line_layout et ajoutez-y votre vue. Voir le code ci-dessous:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
View button = findViewById(R.id.add_ingredient_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup layout = (ViewGroup) findViewById(R.id.ingredients_line_layout);
EditText ingredient = new EditText(YourActivity.this);
ingredient.setHint(R.string.ingredients);
layout.addView(ingredient, 1);
}
});
}
Merci. Cela fonctionne, mais comment puis-je ajouter chaque nouvelle vue EditText dans une ligne différente de mon LinearLayout?
@EdoardoTavilla Regardez cette ligne: layout.addView (ingrédient, 1) . Le deuxième paramètre est une position dans LinearLayout . Modifiez-le pour ajouter une vue à la position souhaitée. Dans mon exemple, j'utilise une valeur constante mais vous pouvez calculer la position de manière dynamique.
Voulez-vous simplement afficher un seul EditText sur le bouton ou voulez-vous en ajouter un nouveau à chaque fois que vous appuyez sur le bouton?
Je veux en ajouter un nouveau à chaque fois que le bouton est enfoncé