2012-07-14 82 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);

您應該能夠將此與您的邏輯一起使用。祝你好運:-)

相關問題