删除动态创建的Editext视图

时间:2017-12-05 11:04:39

标签: android android-edittext

我已使用以下代码

创建了我的观看次数programmatically

创建edittext的方法

 private View newedittext() {
    final EditText editText = new EditText(getActivity());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
    params.setMargins(0, 30, 0, 0);
    editText.setLayoutParams(params);


    editText.setId(count++);
    editText.setTextSize(14);
    editText.setTextColor(Color.rgb(0, 0, 0));
    editText.setMaxEms(2);
    editText.setInputType(InputType.TYPE_CLASS_TEXT);
    return editText;
}

创建imageview的方法

  public ImageView newImageview(Context context) {
    final ImageView imgView = new ImageView(context);
    imgView.setOnClickListener(this);
    imgView.setId(count);
    imgView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_close_red));
    return imgView;
}

在我的oncreateview片段

 Button btnad = (Button) child.findViewById(R.id.btnad);
                btnad.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LinearLayout layout = new LinearLayout(getActivity());
                        layout.setOrientation(LinearLayout.HORIZONTAL);

                        layout.addView(newedittext());
                        layout.addView(newImageview(getActivity()));
                        lnrView.addView(layout);
                    }
                });

点击imageview,我想删除edittext视图

@Override
public void onClick(View v) {
    Toast.makeText(getActivity(), "clicked" + v.getId(), Toast.LENGTH_SHORT).show();

}

enter image description here 如何删除点击中具有特定ID的edittext任何人都可以帮助我

1 个答案:

答案 0 :(得分:1)

您可以在活动中找到此视图,并将其从其父级中删除。

private void removeViewWithId(int id) {
    View view = findViewById(id);
    if(view != null){
        ViewParent parent = view.getParent();
        if(parent instanceof ViewGroup){
            ((ViewGroup)parent).removeView(view);
        }
    }
}

修改

在问题编辑之后,它看起来像你想要删除EditText和ImageView(完整行)。你可以这样做:

@Override
public void onClick(View view) {
    ViewParent parent = view.getParent();
    if (parent instanceof ViewGroup) {
        ((ViewGroup) parent).removeAllViews();
    }
}

如果您只想删除EditText,请将id作为ImageView标记传递:

Button btnad = (Button) child.findViewById(R.id.btnad);
            btnad.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setOrientation(LinearLayout.HORIZONTAL);

        EditText et = newedittext();
        layout.addView(et);
        ImageView iv = newImageview(getActivity())
        iv.setTag(et.getId());
        layout.addView(iv);
        lnrView.addView(layout);
    }
});

然后在onImageViewClick中删除它:

 @Override
    public void onClick(View view) {
        Object editTextId = view.getTag();
        if(editTextId == null){
            return;
        }

        ViewParent parent = view.getParent();
        if (parent instanceof ViewGroup) {
            EditText et = ((ViewGroup) parent).findViewById((Integer) editTextId);            
            ((ViewGroup) parent).removeView(et);
        }
    }