Add edittext dynamically and retrieve values in android

后端 未结 2 1984
北海茫月
北海茫月 2020-12-30 14:46

i added Edit Text dynamically but not get values ,get values for last Edit Text . please help me...

相关标签:
2条回答
  • 2020-12-30 15:02

    Here is the answer :

      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());
                    }
    
                }
            });
    
    0 讨论(0)
  • 2020-12-30 15:08
    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;
    
        }   
    
    }
    
    0 讨论(0)
提交回复
热议问题