【问题标题】:I need to add an EditText field with relative button when clicking on a "+" button单击“+”按钮时,我需要添加带有相关按钮的 EditText 字段
【发布时间】:2012-07-14 02:06:54
【问题描述】:

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

还有其他方法吗?

【问题讨论】:

    标签: android layout button onclick layout-inflater


    【解决方案1】:

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

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

    您可能希望将成分更改为 LinearLayout。添加视图比相对布局更容易,因为您只需将视图添加到 LinearLayout 而不必担心它们的相对位置。

    祝你好运。

    【讨论】:

    • 我还有一个问题。充气效果很好,我可以动态添加我想要的东西。现在,正如我在第一篇文章中所描述的,我需要在每个膨胀的编辑文本字段旁边连接“-”按钮。我该怎么做?我找不到类似的问题...
    • 首先,您需要为每个膨胀视图指定一个 id (setId()),这样您就可以检索和删除它。将每个带有移除 id 的按钮的标签设置为膨胀视图的 id。然后,您需要为 id 为 remove 的按钮每次设置 onclicklistner。在您的 onclick 方法中,检查视图是否删除了 id,如果是,则获取标签并使用它来删除视图。祝你好运。
    【解决方案2】:

    在你的“+”按钮的 onClickListener 中写下这样的内容:

    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); 将其删除

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

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 2021-07-29
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多