如何从我的动态EditText中获取值

时间:2014-10-19 19:15:32

标签: android android-layout

我需要创建用于添加位置的动态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);

                         }
     });


    }

}

2 个答案:

答案 0 :(得分: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();
    }

答案 1 :(得分:0)

这是如何动态添加编辑文本并获取它们的值。

此代码示例向屏幕添加了 4 个编辑文本和按钮。单击这些按钮中的每一个都会为您提供附加到它的编辑文本的值。

 private void addContentToLayout() {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    Button[] buttons = new Button[4];
    EditText[] editTexts = new EditText[4];
    LinearLayout[] ly = new LinearLayout[4];

    for (int i = 0; i < 4; i++) {
        ly[i] = (LinearLayout) inflater.inflate(R.layout.content, null);
        binding.lyContent.addView(ly[i]);
        buttons[i] = (Button) ly[i].findViewById(R.id.btnOk);
        editTexts[i] = (EditText) ly[i].findViewById(R.id.edNo);

        int finalI = i;
        buttons[i].setOnClickListener(v -> {
            Toast.makeText(this, "This is " + editTexts[finalI].getText().toString(), Toast.LENGTH_SHORT).show();
        });

    }
}