如何通过单击按钮添加布局并标识每个元素

时间:2012-05-15 06:31:52

标签: android android-layout android-contacts viewgroup

这是我的形象

Here is my image

enter image description here

如何在点击“+​​”按钮时添加一个新的布局PROGRAMATICALLY(带一个按钮和一个EditText),并为每个孩子设置一个唯一的ID(如图中所示)。

3 个答案:

答案 0 :(得分:2)

private Button buttonItemArray[] = new Button[50];
private EditText edtValueArray[] = new EditText[50];


            int itemId = itemCounter;
            int valueId = valueCounter;

            LinearLayout linearLayout = new LinearLayout(
                    getApplicationContext());
            linearLayout.setOrientation(0);

            buttonItemArray[j] = new Button(NewVendorDetailActivity.this);
            buttonItemArray[j].setLayoutParams(edtName.getLayoutParams());
            buttonItemArray[j].setId(itemId);
            buttonItemArray[j].setText(name);

                    edtValueArray[j] = new EditText(NewVendorDetailActivity.this);
        edtValueArray[j].setLayoutParams(edtName.getLayoutParams());
        edtValueArray[j].setId(valueId);
           edtValueArray[j].setBackgroundResource(android.R.drawable.editbox_background_normal);
        edtValueArray[j].setSingleLine(true);
        edtValueArray[j].setText(value);

        linearLayout.addView(txtItemArray[j]);
        linearLayout.addView(edtValueArray[j]);



            layout.addView(linearLayout);
            itemCounter++;
            valueCounter++;
            j++;

尝试此类型添加按钮自定义..........

答案 1 :(得分:0)

答案 2 :(得分:0)

在xml中创建一个按钮              并在Activity中调用xml 按钮联系人=(按钮)findViewById(R.id.contacts); 然后为按钮创建一个onclick监听器 contacts.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Contacts.Intents.Insert.ACTION,
                    Contacts.People.CONTENT_URI);
            intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
            intent.putExtra(ContactsContract.Intents.Insert.NAME, entryname);
            intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
            startActivity(intent);
            finish();
        }
    });