-1

我是android的新手,我在单击添加新按钮时使用以下代码动态创建了editText。是否可以在editText附近添加一个删除按钮,以便每次单击删除时都会删除相应的editText?

btnAddNew.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
            EditText newPass = new EditText(getApplicationContext());
            allEds.add(newPass);
            newPass.setHint("Name of Label");
            newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            newPass.setWidth(318);
            newPass.setTextColor(Color.parseColor("#333333"));
            newPass.setId(textb);

            rAlign.addView(newPass);
            MY_BUTTON ++;               
            addSpinner();               
        }
    });
4

1 回答 1

0

是的,在 EditText 的同时创建删除按钮,并removeView()像方法一样使用addView()方法,可能是这样的:

btnAddNew.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        ...
        rAlign.addView(newPass);
        MY_BUTTON ++;               
        addSpinner();

        Button btnRemoveOld = new Button(this);
        btnRemoveOld.setId(32); // arbitrary number
        btnRemoveOld.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
                rAlign.removeView(findViewById(textb));
            }
        });     

        // You will need to set parameters to define how the button looks
        //   and where it is in relation to the EditText here
        rAlign.addView(btnRemoveOld);
    }
});
于 2012-09-30T18:03:13.623 回答