2014-10-19 73 views
0

我需要創建動態edittext來添加locations.when用戶單擊添加按鈕它將創建另一個編輯text.i已成功創建動態edittext,但我不知道如何獲取值?幫我如何從我的動態EditText獲取值

package com.example.myone; 

import java.util.ArrayList;`` 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    EditText textIn; 
    Button buttonAdd; 
    LinearLayout container; 
    private Button done; 
    private TextView tv; 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.updatevehicle); 

    final EditText locations; 
    final EditText startingpoint; 
    final EditText endingpoint; 

    int count = 0; 
    final EditText ed[] = new EditText[count]; 
    Button buttonAdd; 
    Button buttonupdate; 
    final LinearLayout container; 
    startingpoint = (EditText) findViewById(R.id.startingpoint); 
    endingpoint = (EditText) findViewById(R.id.endingpoint); 

    locations = (EditText)findViewById(R.id.textin); 
    buttonAdd = (Button)findViewById(R.id.add); 
    buttonupdate = (Button)findViewById(R.id.buttonupdate); 

    container = (LinearLayout)findViewById(R.id.container); 

    buttonAdd.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 
     LayoutInflater layoutInflater = 
     (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View addView = layoutInflater.inflate(R.layout.row, null); 
     TextView textOut = (TextView)addView.findViewById(R.id.textout); 



     textOut.setText(locations.getText().toString()); 
     Button buttonRemove = (Button)addView.findViewById(R.id.remove); 
     buttonRemove.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
     ((LinearLayout)addView.getParent()).removeView(addView); 
     }}); 

     container.addView(addView); 
     }}); 

    final String[] array = new String[container.getChildCount()]; 
    for (int i=0; i < container.getChildCount(); i++){ 
     EditText editText = (EditText)container.getChildAt(i); 
     array[i] = editText.getText().toString(); 
    } 

    buttonupdate.setOnClickListener(new View.OnClickListener() {   
     public void onClick(View view) { 
      String name = startingpoint.getText().toString(); 
      String email = endingpoint.getText().toString(); 
      System.out.println(Arrays.toString(array));     //String password = inputPassword.getText().toString(); 
      // new MyAsyncTask().execute(name, email, password); 
      //UserFunctions userFunction = new UserFunctions(); 
      // JSONObject json = userFunction.registerUser(name, email, password); 

         } 
    }); 


    } 

}

+0

你已經從EditText上獲得的價值** textIn.getText()的toString()** – santalu 2014-10-19 19:17:55

+0

我需要獲取字符串數組中的很多edittext值 – user3675519 2014-10-19 19:22:18

回答

2

你應該做這樣的事情:

String[] array = new String[container.getChildCount()]; 

    for (int i=0; i < container.getChildCount(); i++){ 
     EditText editText = (EditText)container.getChildAt(i); 
     array[i] = editText.getText().toString(); 
    } 
+0

當我打印數組時,它顯示空字符串數組 – user3675519 2014-10-19 19:47:03

+0

請更新您的代碼,看看你如何使用它 – 2014-10-20 08:10:19

+0

請檢查更新的代碼。 – user3675519 2014-10-22 19:07:43

相關問題