2012-07-14 75 views
0

我的问题的标题可能不是很清楚。 我正在为大学项目开发​​我的第一个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); 
    } 

有一些其他的方式?

回答

0

在您的“+”按钮的onClickListener中,您需要使用LayoutInflater将您的配料布局充气到视图中。然后使用findViewById()获取对您的成分RelativeLayout的引用并添加新的View。

确保您为膨胀的新视图指定LayoutParams。

您可能想要将配料更改为LinearLayout。添加视图比RelativeLayout更容易,因为您可以将视图添加到LinearLayout而不必担心它们的相对位置。

祝你好运。

在onClickListener
+0

好...它的工作!谢谢Flynn! – 2012-07-14 16:32:19

+0

我还有什么问题。 充气完美,我可以动态添加我想要的东西。现在,正如我在第一篇文章中所描述的,我需要连接每个膨胀的编辑文本字段旁边的“ - ”按钮。我该怎么做? 我找不到类似的问题... – 2012-07-15 10:29:48

+0

首先,你需要给每个充气视图一个id(setId()),这样你可以检索和重新调整它。使用remove id将每个按钮的标签设置为充气视图的ID。然后,您需要每次都设置onclicklistner来使用id为remove的按钮。在您的onclick方法中,检查视图是否具有id remove,然后如果是,则获取标记并使用它来移除视图。祝你好运。 – Flynn81 2012-07-15 11:18:23

0

到您的 '+' 按钮,写这样的事情:

int et_counter=1; 
LinearLayout ll = (LinearLayout) findViewById(R.id.ll); 

EditText et = new EditText(RecipeActivity.this); 
//assign them an ID with an int counter 
//use this counter later to refer to the EditText 
//to retrieve the values 
et.setId(et_counter); 

ll.addView(et); 

你可以检索这样的价值观:

for (n = 1; n <= et_counter; n++){ 
    et = (EditText) findViewById(n); 
    String ingridient=et.getText().toString(); 
    //use the text somewhere 
} 

同样可以一个onClick监听器分配给你的“ - '按钮,使用et_counter使用ID参考EditText并将其删除使用et.setVisibility(View.GONE);

您应该能够将此与您的逻辑一起使用。祝你好运:-)

相关问题