动态添加edittext并在android中检索值

时间:2011-08-16 11:11:09

标签: android

我动态添加了编辑文本但没有获取值,获取最后一个编辑文本的值。 请帮帮我......

2 个答案:

答案 0 :(得分:10)

public class EnterText extends Activity {

    Button btnMyLine,btnSave;
    LinearLayout LLEnterText;       
    int _intMyLineCount;

     private List<EditText> editTextList = new ArrayList<EditText>();
     private List<TextView> textviewList=new ArrayList<TextView>();
     private List<LinearLayout> linearlayoutList=new ArrayList<LinearLayout>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entertext);



        LLEnterText=(LinearLayout) findViewById(R.id.LlTitle);

        //LLEnterText.setOrientation(LinearLayout.VERTICAL);
        btnMyLine=(Button) findViewById(R.id.btnMyLines);       
        btnSave=(Button) findViewById(R.id.btnSave);


        btnMyLine.setOnClickListener(new OnClickListener() {        
            @Override
            public void onClick(View v) {                       
                    LLEnterText.addView(linearlayout(_intMyLineCount));
                    _intMyLineCount++;
                }
        });     

    btnSave.setOnClickListener(new OnClickListener() {      
            @Override
            public void onClick(View v) {   
                if (ETTitleEnterText.getText().length() == 0)
                {
                    Toast.makeText(EnterText.this, "Please Enter Full Details", Toast.LENGTH_LONG).show();
                }else{

                        for (EditText editText : editTextList) {                            
                            StartTabHost.VARClass._ArrLinesDetails.add(editText.getText().toString());
                        }   
                        for(TextView textview:textviewList){
                            StartTabHost.VARClass._ArrLinesTitle.add(textview.getText().toString());
                        }
                        for(int i=0;i<StartTabHost.VARClass._ArrLinesTitle.size();i++)
                    {
                    Log.d("LinesTitle",StartTabHost.VARClass._ArrLinesTitle.get(i));
                    Log.d("LinesDetails",StartTabHost.VARClass._ArrLinesDetails.get(i));
                    }               

                }

            }
        });
    }

private EditText editText(int _intID) {
            EditText editText = new EditText(this);
            editText.setId(_intID);
            editText.setHint("My lines");
            editText.setWidth(180);         
            editText.setBackgroundColor(Color.WHITE);
            editTextList.add(editText);
            return editText;
        }
    private TextView textView(int _intID)
    {
        TextView txtviewAll=new TextView(this);
        txtviewAll.setId(_intID);
        txtviewAll.setText("My lines:");        
        txtviewAll.setTextColor(Color.RED);
        txtviewAll.setTypeface(Typeface.DEFAULT_BOLD);
        textviewList.add(txtviewAll);
        return txtviewAll;
    }
    private LinearLayout linearlayout(int _intID)
    {
        LinearLayout LLMain=new LinearLayout(this);
        LLMain.setId(_intID);       
        LLMain.addView(textView(_intID));
        LLMain.addView(editText(_intID));
        LLMain.setOrientation(LinearLayout.HORIZONTAL);
        linearlayoutList.add(LLMain);
        return LLMain;

    }   

}

答案 1 :(得分:0)

以下是答案:

  btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < editTextList.size(); i++) {
                    System.out.println("Content"+editTextList.get(i).getText().toString());
                }

            }
        });