【问题标题】:Generating Edit Text Programmatically in Android在 Android 中以编程方式生成编辑文本
【发布时间】:2012-10-20 06:17:11
【问题描述】:

我正在开发联系人应用程序,它添加了电子邮件地址、电话号码。我必须在代码本身中动态创建编辑文本。我不知道如何以及在哪里实现这个逻辑,建议任何帮助将不胜感激。

【问题讨论】:

    标签: android android-edittext


    【解决方案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 方法,以及介于两者之间的所有内容。

    有一个更通用的示例in this question,以及这些进程的一个很好的概要in this blog

    【讨论】:

      【解决方案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);
      

      【讨论】:

        【解决方案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);
        

        【讨论】:

          【解决方案4】:

          LinearLayout 中的EditText。

          试试这个。

          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 );
          

          【讨论】:

            【解决方案5】:
            EditText emailEditText = new EditText(this); //you can pass context based on your function.
            emailEditText.setHint("Enter Email address:"); //works same as in XML file hint
            emailEditText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);  
            yourLayout.addView(emailEditText);
            

            根据您的要求设置您的LayoutLayoutParams

            setContentView(your layout, your layout params)
            

            numberEditText 相同,只是将输入类型更改为 TYPE_CLASS_NUMBER

            【讨论】:

              猜你喜欢
              • 2012-02-27
              • 2013-11-11
              • 1970-01-01
              • 2012-06-07
              • 2014-04-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多