Android: How do you create an EditText field in ja

2019-07-19 17:18发布

I was just wondering how to create an editText field on the click of a button. Is it possible? I cant find anything online. If anyone knows how to do this please answer! and if you know how to configure the size, placement ect also include that information.

3条回答
太酷不给撩
2楼-- · 2019-07-19 17:58

Better still don't set it to invisible but to set it to "gone" you do this in the xml by adding the line

android:visibility="gone"<br/>

If you add

android:visibility="visible"<br/>

the EditText will still take up space but be invisible whereas gone means that not only is is invisible but it is gone and not taking up space

查看更多
Bombasti
3楼-- · 2019-07-19 18:06

Set it invisible where you want the EditText? Can be one of the first things you do in your Activity. And use the button to set it visible.

EditText edtext = (EditText) findViewById(R.id.edtext);
edtext.setVisibility(View.GONE);
...
button.setOnClickListener(new OnClickListener()
{
        @Override
        public void onClick(View view) 
        {
            edtext.setVisibility(View.VISIBLE);
        }
    });

You really should do some basic work first before requesting help here, regarding size and placement etc, which is done in xml mostly.

查看更多
时光不老,我们不散
4楼-- · 2019-07-19 18:16
import android.widget.Button;
import android.widget.EditText;    
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout = (LinearLayout)findViewById(R.id.mylinearlayout);

Button lButton = (Button)findViewById(R.id.mybtnid);
lButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        EditText lEditText = new EditText(this);
        lEditText .setLayoutParams(
            new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
                                          LayoutParams.WRAP_CONTENT));
        lEditText.SetText("Text Here");
        mLinearLayout.addView(lEditText);
        lEditText.setWidth(width);     // change width
        lEditText.setHeight(height);   // change height
        lEditText.setX(<x value>);     // set absolute position of x
        lEditText.setY(<y value>);     // set absolute position of y
    }
}

also you can use

int X = 50; // Arbitrary values - use whatever you want
int Y = 100;

lEditText.setPadding(X, Y, 0, 0);  // set x and y using padding
查看更多
登录 后发表回答