添加删除选项动态生成EDITTEXT [英] Add delete option with dynamically generated editText

查看:250
本文介绍了添加删除选项动态生成EDITTEXT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的新的和我有以下code创建EDITTEXT动态的同时单击添加新button.Is可以添加删除按钮EDITTEXT附近,这样每个同时单击删除相应EDITTEXT将被删除?

  btnAddNew.setOnClickListener(新OnClickListener(){        公共无效的onClick(视图v){            的LinearLayout rAlign =(的LinearLayout)findViewById(R.id.lId);
            的EditText newPass =新的EditText(getApplicationContext());
            allEds.add(newPass);
            newPass.setHint(标签名称);
            newPass.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            newPass.setWidth(318);
            newPass.setTextColor(Color.parseColor(#333333));
            newPass.setId(TEXTB);            rAlign.addView(newPass);
            MY_BUTTON ++;
            addSpinner();
        }
    });


解决方案

是的,在同一时间,因为你的EditText创建删除按钮并使用 removeView()方法就像 addView()方法,也许是这样的:

  btnAddNew.setOnClickListener(新OnClickListener(){
    公共无效的onClick(视图v){
        ...
        rAlign.addView(newPass);
        MY_BUTTON ++;
        addSpinner();        按钮btnRemoveOld =新按钮(本);
        btnRemoveOld.setId(32); //任意数量
        btnRemoveOld.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                的LinearLayout rAlign =(的LinearLayout)findViewById(R.id.lId);
                rAlign.removeView(findViewById(TEXTB));
            }
        });        //你将需要设置参数来定义如何按钮看起来
        //以及它是相对于的EditText这里
        rAlign.addView(btnRemoveOld);
    }
});

I'm new in android and i have created editText dynamically with the following code while clicking add new button.Is it possible to add a delete button near editText so that each while clicking the delete respective editText will be removed?

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();               
        }
    });

解决方案

Yes, create your remove button at the same time as your EditText and use the removeView() method just like the addView() method, maybe like this:

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);
    }
});

这篇关于添加删除选项动态生成EDITTEXT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆