1

ユーザーが情報を挿入する必要がある入力テキストと、テキストを入力するためのボタン「+」を備えたアプリケーションがあります。ユーザーが「+」ボタンを押すと、別のテキスト入力と別の「+」ボタンがこのボタンの横に動的に表示されるように、フォームを動的にしたいと思います。プロセスは同じ方法で繰り返されます。

xml ファイル sample_content を作成しました。

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/attempt"
                android:layout_alignParentBottom="true"
                android:text="TextView" />

            <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="36dp"
                android:layout_height="32dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="22dp"
                android:text="+" />

            <EditText
                android:layout_width="229dp"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginRight="14dp"
                android:layout_toLeftOf="@+id/addKey"
                android:background="@drawable/inputtext_corner"
                android:ems="10"
                android:textSize="18sp" >

                <requestFocus />

            </EditText>

        </RelativeLayout>

私のアクティビティでは、 AddDeviceActivity を入れました:

                inflater = LayoutInflater.from(AddDeviceActivity.this);
                Button addKey = (Button) findViewById(R.id.addKey);

                addKey.setOnClickListener(new Button.OnClickListener() {
                    public void onClick(View v) {
                        final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
                        final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
                        //  TODO: Look up the 5 different signatures of the addView method, 
                        //  and pick that best fits your needs
                        canvas.addView(childView);
                    }
                });

しかし、最初の入力テキストと最初のボタンを追加するときに、AddDeviceActivity で 2 番目のボタンを動的に機能させる方法がわからないため、このソリューションは機能しません。

4

2 に答える 2

0

これができるかどうか疑問に思っています:

アクティビティを実装OnClickListenerして、このメソッドをアクティビティに追加します。

public void onClick(View v) {
    final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
    final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
    canvas.addView(childView);
    ((Button)childView.findViewById(R.id.addKey)).setOnClickListener(AddDeviceActivity.this);
}

そして、使用する初期コードを変更します

addKey.setOnClickListener(this);

匿名の内部クラスの代わりに。

私はこれをテストしていませんが、なぜ機能しないのかわかりません。

于 2012-04-12T10:06:33.797 に答える
0

これをチェックして、メソッドのオブジェクトnullの代わりに渡しますcanvasinflate()

 addKey.setOnClickListener(new Button.OnClickListener() {
                        public void onClick(View v) {
                            final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
                            final View childView = inflater.inflate(R.layout.sample_component, null, false);
                            //  TODO: Look up the 5 different signatures of the addView method, 
                            //  and pick that best fits your needs
                            canvas.addView(childView);
                        }
                    });
于 2012-04-12T11:50:40.217 に答える