簡體   English   中英

Android Studio-只需單擊一個按鈕即可創建EditText

[英]Android Studio - Create an EditText with a click of a button

似乎無法在網上找到可以解釋使用按鈕添加新EditText字段的帖子/視頻。 我以后需要使用edittexts。 有人可以向我解釋如何創建此系統嗎? 或鏈接解釋此問題的視頻/帖子。 我已經搜尋了很長時間,但仍然找不到很好的解釋。 謝謝。

Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText t = new EditText(myContext);
        t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        root.addView(t);
    } 
});

root:是您要在其中添加EditText的根布局。

使用以下代碼

添加此Java文件。

 LinearLayout linearLayout = findViewById(R.id.editTextContainer);  


    Button btnShow = findViewById(R.id.btnShow);
    if (btnShow != null) {
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 // Create EditText
        final EditText editText = new EditText(this);
       editText.setHint(R.string.enter_something);
       editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
       editText.setPadding(20, 20, 20, 20);

    // Add EditText to LinearLayout  
    if (linearLayout != null) {
        linearLayout.addView(editText);
    }
            }
        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM