如何在编辑文本可以动态地在Android中创建 [英] How an Edit Text can be created dynamically in Android

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

问题描述

我是新到Android编程和我有动态创建的文本字段的问题;

我想创建一个视图,其中有一个名为按钮创建文本框和两个编辑文本一个编辑文本的名字是行和第二编辑文本的名字是列。作为一个用户在两个编辑文本让数字说ROW = 2和列= 3,preSS按钮创建文本字段它可能产生的创建文本字段按钮下方6编辑文本。这些6-编辑文本应定位在2行3列如以下

的EditText-1的EditText-2的EditText-3

的EditText-1的EditText-2的EditText-3


解决方案

 的setContentView(R.layout.main);
        LL的LinearLayout =(的LinearLayout)findViewById(R.id.ll);
        显示显示=((窗口管理器)getApplicationContext()getSystemService(Context.WINDOW_SERVICE)。)getDefaultDisplay()。
        INT宽度= display.getWidth()/ 3;
        对(INT I = 0; I&2;我++){
            的LinearLayout L =新的LinearLayout(本);
            l.setOrientation(LinearLayout.HORIZONTAL);
            为(中间体J = 0; J&下; 3; J ++){
                等的EditText =新的EditText(本);
                的LayoutParams LP =新的LayoutParams(宽度,LayoutParams.WRAP_CONTENT);
                l.addView(ET,LP);
          }
            ll.addView(升);
        }

使用上述code在你的onCreate()这里的主力是XML文件。并且可以取代我2和j 3;用的EditText 1安培值; 2。

我现在用的样品的main.xml低于::

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / LL
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>
    <的EditText
        机器人:ID =@ + ID / ET1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>        < requestFocus的/>
    < /&的EditText GT;
    <的EditText
        机器人:ID =@ + ID / ET2
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
    <按钮
        机器人:ID =@ + ID / BTN。
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=按钮/>< / LinearLayout中>

I am new to android programming and I have a problem of creating text fields dynamically;

I want to create a view in which there is a button named "Create text fields" and two Edit Text one Edit Text name is ROW and the second edit text name is COLUMN. As a user enters numbers in the both Edit Text let say ROW =2 and COLUMN =3 and press the button "Create text fields" it may create 6 Edit Text below the button of "Create text fields". These 6-edit text should be positioned in 2 rows and 3 columns like below

EditText-1 EditText-2 EditText-3

EditText-1 EditText-2 EditText-3

解决方案

setContentView(R.layout.main);
        LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
        Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int width = display.getWidth()/3;
        for(int i=0;i<2;i++){
            LinearLayout l = new LinearLayout(this);
            l.setOrientation(LinearLayout.HORIZONTAL);
            for(int j=0;j<3;j++){
                EditText et = new EditText(this);
                LayoutParams lp = new LayoutParams(width,LayoutParams.WRAP_CONTENT);
                l.addView(et,lp);
          }
            ll.addView(l);
        }

use the above code in your onCreate() where "main" is xml file. and you can replace i<2 and j<3 with values of edittext 1 & 2.

sample main.xml which i am using is below::

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>


    <EditText
        android:id="@+id/et2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

这篇关于如何在编辑文本可以动态地在Android中创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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