0

这是我的图像

这是我的图像

在此处输入图像描述

如何在单击“+”按钮时以编程方式添加新布局(带有按钮和 EditText),并为每个孩子设置一个唯一的 ID(如图所示)。

4

3 回答 3

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++;

试试这种类型的添加按钮自定义............

于 2012-05-15T06:41:32.903 回答
0

请阅读以下内容,

http://www.learn-android.com/2010/01/05/android-layout-tutorial/

于 2012-05-15T06:41:16.793 回答
0

在 xml 中创建一个按钮,并在 Activity 中调用 xml,如下所示 Button contacts = (Button) findViewById(R.id.contacts); 然后为按钮 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();
        }
    });
于 2012-05-15T06:47:54.113 回答