0

おそらく私の質問のタイトルはあまり明確ではありません。大学のプロジェクトで初めての Android アプリケーションを開発しています。私が今必要としているのは、データベースでレシピを探すために、ユーザーが 1 つの「材料」を入力できるようにすることです。かんたんだよ。

おそらくこのウェブサイトの同様の投稿をすべて読みましたが、問題を解決できませんでした。最初の EditText フィールドの下に「+」ボタンが表示されるように、ユーザーが複数の成分を指定できるようにしたいと考えています。最初のフィールドとボタン自体の間に新しい EditText フィールドを追加します。EditText フィールドとともに、クリックすると相対入力フィールドを「非表示」にする「-」ボタンが必要です。

説明が明確であることを願っています。

私が今持っているのは、メインの XML レイアウトと、edittext と「-」ボタンを備えた 2 番目のレイアウトです...しかし、メインのレイアウトでこのレイアウトをプッシュ/インフレート/表示する方法がわかりませんでした...

手伝っていただけませんか?これは私がプッシュしたいレイアウトです:

<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);
    }

他の方法はありますか?

4

2 に答える 2

0

「+」ボタンの onClickListener では、LayoutInflater を使用して、成分レイアウトをビューにインフレートする必要があります。次に、findViewById() を使用して成分 RelativeLayout への参照を取得し、それに新しいビューを追加します。

インフレートされた新しいビューには、必ず LayoutParams を指定してください。

成分を LinearLayout に変更したい場合があります。相対位置を気にせずにビューを LinearLayout に追加するだけでよいため、RelativeLayout よりもビューを追加する方が簡単です。

幸運を。

于 2012-07-14T12:38:11.903 に答える
0

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);

これをロジックで使用できるはずです。幸運を :-)

于 2012-07-15T10:59:35.853 に答える