删除edittext视图

时间:2014-01-22 09:27:08

标签: android android-edittext

当我点击“添加”按钮时,顶部会添加一个edittext视图,我希望当我点击“删除”按钮时,焦点的edittext视图会被删除

layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
        btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
        btn_addmore_cntct.setOnClickListener(OnClick());
        EditText editview = new EditText(this);
        editview.setText("Add more");

    }


    private OnClickListener OnClick() {
        // TODO Auto-generated method stub
        // changing return type "null" to "new OnClickListner"
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText tab = new EditText(getApplicationContext());
                tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                // setting id for edit-text views
                tab.setId(id);
                // and increment
                id++;

                // adding (0) to display edit-text view on the top
                layoutLinear.addView(tab, 0);
                tab.requestFocus();

            }
        };

    }

2 个答案:

答案 0 :(得分:0)

请参阅removeView()方法。或者参见this方法。 或者你可以使它不可见yuorTextView.setVisibility(View.GONE)。

答案 1 :(得分:0)

it may help to u..   

 public void addrow() {

            final View view = getLayoutInflater().inflate(R.layout.graphrow,
                    layout_addrow, false);
              LinearLayout layout_back = (LinearLayout) view
                    .findViewById(R.id.linearLayout1);
            ImageView delete = (ImageView) view.findViewById(R.id.delete);
            ImageView cancel = (ImageView) view.findViewById(R.id.cancel);
            final EditText ed_item = (EditText) view.findViewById(R.id.items);
            final EditText ed_value = (EditText) view.findViewById(R.id.values);
            final EditText ed_value1 = (EditText) view.findViewById(R.id.values1);

            if (Type_Multi.equals("Single")) {
                ed_value1.setVisibility(View.GONE);
            } else {
                ed_value1.setVisibility(View.VISIBLE);
            }
    //      row_cnt = layout_addrow.getChildCount();
    //       if(row_cnt==1 || row_cnt==3 || row_cnt==5 || row_cnt==7 ||
    //       row_cnt==9)
    //       {
    //           layout_back.setBackgroundColor(Color.parseColor("#70A0A0A5"));
    //       }else
    //       {
    //           layout_back.setBackgroundColor(Color.WHITE);
    //       }

            delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    layout_addrow.removeView(view);
    //              
                }
            });
            cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    ed_item.getText().clear();
                    ed_value.getText().clear();
                    ed_value1.getText().clear();

                }
            });

            layout_addrow.addView(view);

        }