ユーザーが情報を挿入する必要がある入力テキストと、テキストを入力するためのボタン「+」を備えたアプリケーションがあります。ユーザーが「+」ボタンを押すと、別のテキスト入力と別の「+」ボタンがこのボタンの横に動的に表示されるように、フォームを動的にしたいと思います。プロセスは同じ方法で繰り返されます。
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 番目のボタンを動的に機能させる方法がわからないため、このソリューションは機能しません。