简体   繁体   English

如何在Android中单击按钮时添加未知数量的edittext

[英]How to add unknown number of edittext on button click in android

In my android application, I have an edittext and below that there are two buttons. 在我的android应用程序中,我有一个edittext,下面有两个按钮。 The first one is add button and another is send button. 第一个是添加按钮,另一个是发送按钮。 When user clicks on add button, then it should create another edittext just below the first one. 当用户单击添加按钮时,它应该在第一个编辑文本的正下方创建另一个编辑文本。 This is fine if I have to create known number of edittexts and I can use for loop like this : 如果我必须创建已知数量的edittext,并且可以使用如下所示的for循环,那就很好了:

for(int i = 0; i < 20; i++) 
{
    Edittext et= new Edittext(this);
    ll.addView(et);
}

But the problem is if user clicks multiple times then it should be insert multiple edtitexts there which is unknown. 但是问题是,如果用户单击多次,则应该在其中插入多个未知的文本。 Means user can click 100's or 200's or more times. 表示用户可以单击100或200或更多次。 So don't know exactly how many times user will click that add button. 因此,不知道用户将单击该添加按钮多少次。 Can anybody please help me to how to do this..?? 有人可以帮我怎么做吗?

It is simple. 很简单。 Add one click listener and your job done. 添加一键式侦听器,您的工作就完成了。 Hope it will help you. 希望对您有帮助。

Button addButton= (Button) findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       Edittext et= new Edittext(your_activity_name.this); //Replace with your activity name
       ll.addView(et);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM