简体   繁体   English

单击按钮时创建一个EditText

[英]Creating an EditText when a Button is clicked

I want to create an onclick event that creates an EditText . 我想创建一个创建EditTextonclick事件。 I have tried the following code but it erases everything and creates a new Layout with a EditText . 我尝试了以下代码,但它擦除了所有内容,并使用EditText创建了一个新的Layout。

public class MainActivity extends Activity {

    ArrayList<Contact> contact;
    Contact currentcontact;
    EditText nameArea,emailArea,phoneArea;
    int emails=1;
    int phones=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameArea=findViewById(R.id.name_area);
        emailArea=findViewById(R.id.e1);
        phoneArea=findViewById(R.id.p1);
        contact=new ArrayList<>();
    }

    public void buttonclick(View v){
        if(v.getId()==R.id.addemail){
            createemaileditview();
        }
        if(v.getId()==R.id.addphone){
            createphoneeditview();
        }
        if(v.getId()==R.id.save){

        }
        if(v.getId()==R.id.cancel){
        }

    }
    protected void createemaileditview(){
        LinearLayout outerLayout=new LinearLayout(this);
        outerLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        outerLayout.setOrientation(LinearLayout.VERTICAL);
        EditText email=new EditText(this);
        email.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
        outerLayout.addView(email);
        setContentView(outerLayout);
        emails++;
    }
    protected void createphoneeditview(){
        phones++;
    }
}

There is a slight mistake in your code. 您的代码中有一个小错误。 You are replacing your main activity layout with outerLayout by calling setContentView(outerLayout) . 您将通过调用setContentView(outerLayout)外部活动布局替换为setContentView(outerLayout) You can get your desired functionality by adding your editText to your main activity layout instead of creating new layout and replacing all the existing stuff. 通过将editText添加到主活动布局中,而不是创建新的布局并替换所有现有的东西,即可获得所需的功能。 See the link for complete code. 请参阅链接以获取完整代码。 Dynamically add Edittext to a relative layout under an existing editText 动态将Edittext添加到现有editText下的相对布局

If you are only have one EditText to create or you keep want to keep the design under control easily, the better keep them in invisible with View.INVISIBLE at the beginning, not gone by View.GONE. 如果只有一个EditText可以创建,或者您一直希望轻松控制设计,那么最好一开始使用View.INVISIBLE使它们不可见,而不是被View.GONE忽略。 With this your design can be simple. 有了这个,您的设计可以很简单。

From android; 从android;

View.GONE This view is invisible, and it doesn't take any space for layout purposes. View.GONE该视图是不可见的,并且不占用任何空间用于布局。

View.INVISIBLE This view is invisible, but it still takes up space for layout purposes. View.INVISIBLE此视图是不可见的,但仍会占用空间以进行布局。

See 看到

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

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