安卓:你如何创建Java类一个EditText费尔德 [英] Android: How do you create an EditText feild in java class

查看:306
本文介绍了安卓:你如何创建Java类一个EditText费尔德的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何在一个按钮,单击创建EDITTEXT场。可能吗?我不能在网上找到任何东西。如果有人知道如何做到这一点请回答!如果你知道如何配置的大小,位置等还包括信息。

干杯
尼克


解决方案

 进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.LinearLayout;
进口android.widget.LinearLayout.LayoutParams;的LinearLayout mLinearLayout =新的LinearLayout(本);
mLinearLayout =(的LinearLayout)findViewById(R.id.mylinearlayout);按钮lButton =(按钮)findViewById(R.id.mybtnid);
lButton.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(查看为arg0){
        的EditText lEditText =新的EditText(本);
        lEditText .setLayoutParams(
            新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                                          LayoutParams.WRAP_CONTENT));
        lEditText.SetText(文字在这里);
        mLinearLayout.addView(lEditText);
        lEditText.setWidth(宽); //改变宽度
        lEditText.setHeight(高度); //改变高度
        lEditText.setX(小于x值与GT); //设置x的绝对位置
        lEditText.setY(小于y值&GT); //设置Y的绝对位置
    }
}

也可以使用

  INT X = 50; //任意值 - 任何你想要使用
INT Y = 100;lEditText.setPadding(X,Y,0,0); //设置x和y使用填充

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.

Cheers Nick

解决方案

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

这篇关于安卓:你如何创建Java类一个EditText费尔德的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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