3

ユーザーがボタンをクリックしたときにフィールドを追加できるフォーム、つまりaと2LinearLayoutを含むAを作成しようとしています。連絡先アプリに似ています。誰かがチュートリアルや例をお勧めできますか?TextViewEditText

4

2 に答える 2

2

必要な動的ビューを含む線形レイアウトを作成し、動的に追加するレイアウトファイルを作成し、そのレイアウトをアクティビティに拡張するためのクラスを作成し、そのオブジェクトを使用してビューを取得し、そのボックスに追加します(レイアウト動的ビューが必要です)。あなたはこのように行くことができます。

public class BoxView {
    private Context context;
    private TextView tv;
    private EditText edt;
    private Button btn;
    private View v;

    public BoxView(Context context) {

        // TODO Auto-generated constructor stub
        this.context= context;
        init();
    }

    private void init() {
        // TODO Auto-generated method stub
        LayoutInflater inflator= LayoutInflater.from(context);
         this.v= inflator.inflate(R.layout.sample_layout, null);

        this.tv=(TextView)v.findViewById(R.id.textView1);
        this.edt=(EditText)v.findViewById(R.id.editText1);
        this.btn=(Button)v.findViewById(R.id.button1);

    }

    public View getView(){
        return v;
    }
    public void setTv(TextView tv) {
        this.tv = tv;
    }
    public TextView getTv() {
        return tv;
    }
    public void setEdt(EditText edt) {
        this.edt = edt;
    }
    public EditText getEdt() {
        return edt;
    }
    public void setBtn(Button btn) {
        this.btn = btn;
    }
    public Button getBtn() {
        return btn;
    }


}

そしてあなたの活動ではあなたはこのようにすることができます:

 LinearLayout layout=(LinearLayout)findviewbyId(R.id.layout1);
 BoxView box=new BoxView(context);
 layout.addView(box.getView);
于 2012-06-22T06:38:19.203 に答える
2

新しいコンポーネントをxmlで定義された親レイアウトに追加することで追加できます(IDを指定してそのレイアウトを参照しLinearLayout linearLayout = findViewById(R.id.idOfLayout);、これを使用してアクティビティでアクセスするとLinearLayout、その親レイアウトを取得し、動的に別のレイアウトを追加できますそれらを作成する)

Androidでコンポーネントを動的に追加および削除する方法を理解するのに役立つリンクを次に示します。

コードLinkを介して動的フォームを構築します 。LinearLayoutを追加してElementsLinkを削除します。

于 2012-06-22T06:41:53.503 に答える