Android:如何在Java类中创建EditText字段 [英] Android: How do you create an EditText field in java class

查看:36
本文介绍了Android:如何在Java类中创建EditText字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何通过单击按钮来创建editText字段.可能吗?我在网上找不到任何东西.如果有人知道该怎么做,请回答!并且如果您知道如何配置尺寸,则放置位置等信息也将包含在内.

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.

推荐答案

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

您也可以使用

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

lEditText.setPadding(X, Y, 0, 0);  // set x and y using padding

这篇关于Android:如何在Java类中创建EditText字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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