我的问题的标题可能不是很清楚。 我正在为大学项目开发我的第一个Android应用程序。我现在需要的是给我的用户输入一种“成分”,以便在数据库中查找食谱。这很容易。当我点击一个“+”按钮时,我需要添加一个带有相对按钮的EditText字段
我可能读过这个网站上的所有类似帖子,但我无法解决我的问题:现在我想给用户指定多种配料的可能性,以便我的下方应该有一个“+”按钮第一个EditText字段允许在第一个和按钮之间添加新的EditText字段。 与EditText字段一起,我需要一个“ - ”按钮,单击时可“隐藏”相关输入字段。
我希望描述清楚。
我现在拥有的是我的主要XML布局,第二个是带有edittext和“ - ”按钮......但我不明白如何在主要布局中推/膨胀/显示此布局......
你能帮我吗? 这isthe布局我要推:
<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);
}
有一些其他的方式?
好...它的工作!谢谢Flynn! – 2012-07-14 16:32:19
我还有什么问题。 充气完美,我可以动态添加我想要的东西。现在,正如我在第一篇文章中所描述的,我需要连接每个膨胀的编辑文本字段旁边的“ - ”按钮。我该怎么做? 我找不到类似的问题... – 2012-07-15 10:29:48
首先,你需要给每个充气视图一个id(setId()),这样你可以检索和重新调整它。使用remove id将每个按钮的标签设置为充气视图的ID。然后,您需要每次都设置onclicklistner来使用id为remove的按钮。在您的onclick方法中,检查视图是否具有id remove,然后如果是,则获取标记并使用它来移除视图。祝你好运。 – Flynn81 2012-07-15 11:18:23