0

フォームのように機能するカスタム edittext があります。ラベル、編集テキスト、およびボタンがあります。フォームを繰り返し使用できるように、タグを使用してレイアウトに含めています。ボタンに持たせたい機能は、クリックしたときの形を再現することです。これは、最初のフォームの最初のクリックで可能です。ただし、新しいフォームのボタンをクリックしてもアクションはありません。ただし、最初のフォーム ボタンをクリックすると、新しいフォームが作成されます。これは私の xml: です。

<?xml version="1.0" encoding="utf-8"?>

<TextView android:id="@+id/form_textview"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:textSize="14sp" android:text="Number:" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText android:id="@+id/form_edittext"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton android:id="@+id/form_button"
        android:layout_width="wrap_content" android:layout_height="match_parent"
        android:src="@drawable/ic_menu_example" android:paddingLeft="1dp"
        android:paddingRight="1dp" />
</LinearLayout>

そして、これは私が新しいフォームを動的に追加しようとしている方法です:

public final void onClick(final View view) 
{

    if (view == findViewById(R.id.form_button))
    {
        try{
            LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);

                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View tempView = vi.inflate(R.layout.form, null);
                numberEntryViews.add(tempView);

                layout.addView(tempView);
                ImageButton btn = (ImageButton) tempView.findViewById(R.id.form_button);

                btn.setOnClickListener(this);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                Log.d(TAG, "Failed to inflate");
            }
        }
}

これを実装するためにさまざまな方法を試しましたが、うまくいきません。ヘルプや提案は大歓迎です。

4

2 に答える 2

1

新しく作成したボタンをクリックすると、この行は true になりません

    if (view == findViewById(R.id.form_button))

新しく作成したボタンに固定 ID を割り当てるか、オフセットを使用してみてください。

于 2011-06-28T13:36:30.840 に答える
0

新しいフォームからボタンの機能を更新するには、クラス レベルのボタンとビューを作成する必要があると考えました。

        try{
        LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
        LinearLayout layout2 = (LinearLayout) layout.findViewById(R.id.numbersll);

        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        tempView = vi.inflate(R.layout.form, null);
        numberEntryViews.add(tempView);

        layout2.addView(tempView);
        btn = (ImageButton) tempView.findViewById(R.id.form_button);
        //TODO: Work from here. Ask on Stack Overflow.
        btn.setOnClickListener(this);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        Log.d(TAG, "Failed to inflate");
    }

btn はクラス レベルで、クリックするたびに更新されます。私の場合は理にかなっている最後のボタンから機能を削除します。

于 2011-06-29T09:42:44.143 に答える