возможно, название моего вопроса не совсем понятно. Я разрабатываю свое первое приложение для Android для университетского проекта. Что мне сейчас нужно, так это дать возможность моим пользователям вводить один «ингредиент», чтобы искать рецепты в базе данных. Это просто.
Я прочитал, вероятно, все подобные сообщения на этом веб-сайте, но не смог решить свою проблему: теперь я хочу дать пользователю возможность указать более одного ингредиента, чтобы под моим первым полем EditText была кнопка «+», позволяющая чтобы добавить новые поля EditText между первым и самой кнопкой. Вместе с полем EditText мне нужна кнопка "-", которая при нажатии "скрывает" относительное поле ввода.
надеюсь описание понятно.
То, что у меня есть сейчас, это мой основной XML-макет и второй, с текстом редактирования и кнопкой "-"... но я не понял, как нажимать/раздувать/показывать этот макет в основном...
Не могли бы вы мне помочь? Это макет, который я хочу нажать:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_below="@+id/ingredient"
android:layout_height ="wrap_content"
android:layout_width="fill_parent"
android:visibility="gone"
android:id="@+id/newLine1">
<EditText
android:id="@+id/ingredient"
android:layout_width="100dp"
android:layout_height="50dp"
android:inputType="text"
android:hint="@string/hint_ingredient" />
<Button
android:id="@+id/remove"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="@string/remove"
android:layout_toRightOf="@+id/ingredient"
android:onClick="removeLine"
/>
</RelativeLayout>
Это основной макет:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_linear">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:scrollbars="horizontal"
android:id="@+id/scrollView"
android:layout_weight="1.0" android:fadingEdge="none">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/ingredients">
<EditText
android:id="@+id/ingredient"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hint_ingredient" />
</RelativeLayout>
</ScrollView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height ="wrap_content"
android:layout_width="match_parent"
android:id="@+id/buttons">
<Button
android:id="@+id/add"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/add"
android:text="@string/search"
/>
</RelativeLayout>
</LinearLayout>
Как насчет удаления новых линий, добавленных инфлятором?
Пока решил так:
public void removeLine(View v)
{
View line = (View) v.getParent();
line.setVisibility(View.GONE);
}
Есть ли другой способ?