在Android的编程生成编辑文本(Generating Edit Text Programmati

2019-06-18 14:23发布

我开发的应用联系,这增加了电子邮件地址,电话号码。 我必须在代码本身动态创建编辑文本。 我不知道怎样和在哪里实施这个逻辑,提出任何帮助,将不胜感激。

Answer 1:

你可以像这样创建它:

EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(myEditText);

这可以在任何地方的UI线程上实现; 一个点击监听器,一个onCreate之间的方法等等,应有尽有。

还有一个更通用的例子在这个问题上 ,而这些过程的良好破败,在这个博客 。



Answer 2:

使用下面的代码添加EDITTEXT编程方式,它会解决你的问题。

RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.mRlayout);
RelativeLayout.LayoutParams mRparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
EditText myEditText = new EditText(context);
myEditText.setLayoutParams(mRparams);
mRlayout.addView(myEditText);


Answer 3:

布局

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:gravity="center_horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contact Application"
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_horizontal"/>

//container Layout
    TableLayout tbl=(TableLayout)findViewById(R.id.TableLayout1);
    //table row
    TableRow tr = new TableRow(this);
    TableLayout.LayoutParams tableRowParams=
            new TableLayout.LayoutParams
            (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
    //for set margin
    tableRowParams.setMargins(0, 10, 0, 0);
    tr.setLayoutParams(tableRowParams);
    //text view
    TextView tv=new TextView(this);
    tv.setText("Email");
    tv.setGravity(Gravity.CENTER);
    tv.setTextColor(Color.parseColor("#0070C0"));
    tv.setTextSize(26);
    tv.setLayoutParams(new TableRow.LayoutParams(100, TableRow.LayoutParams.WRAP_CONTENT));
    //add textview
    tr.addView(tv);
    //set layout params of edittext
    TableRow.LayoutParams etParams=
            new TableRow.LayoutParams
            (120,30);
    etParams.setMargins(10, 0, 0, 0);

    EditText et=new EditText(this);
    et.setLayoutParams(etParams);
    //set background
    et.setBackgroundResource(R.drawable.bg_grey);
    et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
    tr.addView(et);

    tbl.addView(tr, tableRowParams);


Answer 4:

在的EditText内的LinearLayout。

试试这个。

LinearLayout linearLayout = new LinearLayout(getContext());
                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                linearLayout.setOrientation(LinearLayout.VERTICAL);
                                layoutParams.setMargins(2, 1, 2, 1);
                                linearLayout.setLayoutParams(layoutParams);

EditText editText = new EditText (getContext());
                                    editText .setTextSize(15);
                                    editText .setLayoutParams(layoutParams);
                                    linearLayout.setGravity(Gravity.CENTER);
                                    editText .setText(Html.fromHtml(directiveMessage));
                                    linearLayout.addView(editText );


文章来源: Generating Edit Text Programmatically in Android