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 中动态工作

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而不是canvas对象inflate()

 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 回答