2012-04-13 73 views
4

我是android編程新手,我有動態創建文本字段的問題;如何在Android中動態創建編輯文本

我想創建一個視圖,其中有一個名爲「創建文本字段」的按鈕和兩個編輯文本一個編輯文本名稱是ROW,第二個編輯文本名稱是COLUMN。當用戶在編輯文本(比如說ROW = 2和COLUMN = 3)中輸入數字並按下「創建文本字段」按鈕時,它可以在「創建文本字段」按鈕下面創建6編輯文本。這6個編輯文字的位置應在2行3列像下面

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

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

+0

你已經嘗試了什麼,秀我們.. – 2012-04-13 06:22:38

回答

1

你這是怎麼動態創建一個EditText中...

希望這是很有用的...

這僅僅是一個例子...您可以參考這個...

EditText t = new EditText(myContext); 
    t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    layout.addView(t); 

final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
final EditText edit_text = new EditText(this); 
edit_text.setLayoutParams(lparams); 
edit_text.setText("New text: " + text); 
4
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); 
     } 

使用上面的代碼在你的onCreate()其中, 「主要」 是XML文件。你可以取代我< 2和j < 3 EditText上的值1 & 2.

裏面我是用

樣品的main.xml低於::

<?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> 
2
public class DynamicEditTextActivity extends Activity { 

    private EditText row, column; 
    private Button submit; 
    private LinearLayout main, matrix; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     main = (LinearLayout) findViewById(R.id.mainlayout); 
     row = (EditText) findViewById(R.id.row); 
     column = (EditText) findViewById(R.id.column);  
     matrix = (LinearLayout) findViewById(R.id.matrix); 
     matrix.setOrientation(LinearLayout.VERTICAL); 

     submit = (Button) findViewById(R.id.submit); 
     submit.setOnClickListener(generate); 
    } 

    OnClickListener generate = new OnClickListener() { 

     public void onClick(View v) { 

      matrix.removeAllViews(); 

      int rows = Integer.parseInt(row.getText().toString()); 
      int cols = Integer.parseInt(column.getText().toString()); 

      for (int i = 0; i < rows; i++) { 

       LinearLayout layout = new LinearLayout(
         DynamicEditTextActivity.this); 
       layout.setOrientation(LinearLayout.HORIZONTAL); 

       for (int j = 0; j < cols; j++) { 

        TextView text = new TextView(DynamicEditTextActivity.this); 
        text.setLayoutParams(new LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT)); 
        text.setText((j+1) + " "); 
        text.setTextColor(Color.RED); 

        layout.addView(text); 
       } 

       matrix.addView(layout); 
      } 
     } 
    }; 
+0

這個例子工作到完美。非常感謝! – 2017-05-02 19:31:53