Générer Edit Text Programatically in Android
Je développe une application de contact, qui ajoute une adresse e-mail, un numéro de téléphone. Je dois créer un texte d'édition dynamiquement dans le code lui-même. Je ne sais pas comment et où mettre en œuvre cette logique, suggère que toute aide serait reconnaissante.
3 Solutions collect form web for “Générer Edit Text Programatically in Android”
Vous pouvez le créer de la manière suivante:
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);
Cela peut être implémenté n'importe où sur le thread UI; Un écouteur de clics, une méthode onCreate
et tout ce qui se trouve entre les deux.
Il y a un exemple plus générique dans cette question et un bon résumé de ces processus dans ce blog .
Utilisez ci-dessous le code pour ajouter Edittext Programatically, il résoudra votre problème.
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);
Disposition
<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"/>
code
//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);