简体   繁体   中英

I inflated a view 5 times, the view which was inflated had a EditText, now i have 5 EditText on the screen, how to get the text from those EditTexts


public class MainActivity extends AppCompatActivity {



    EditText EditText1,EditText2,EditText3,EditText4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        EditText1 = findViewById(R.id.edittext);
        EditText2 = findViewById(R.id.edittext);
        EditText3 = findViewById(R.id.edittext);
        EditText4 = findViewById(R.id.edittext);


        addChildLayout();
        addChildLayout();
        addChildLayout();
        addChildLayout();
    }

    public void addChildLayout(){

        LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       
        LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
      
        View view=layoutInfralte.inflate(R.layout.activity_main_layout, null);
        
        linearLayout.addView(view);

    }
}

how do I select the EditText, from all the four view and display them, any ideas? what can i do here?

I want to get the data from each of the 4 EditTexts

Use .getText() on each editText reference

public class MainActivity extends AppCompatActivity {



    EditText EditText1,EditText2,EditText3,EditText4;
List<EditText> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        EditText1 = findViewById(R.id.edittext);
        EditText2 = findViewById(R.id.edittext);
        EditText3 = findViewById(R.id.edittext);
        EditText4 = findViewById(R.id.edittext);


        addChildLayout();
        addChildLayout();
        addChildLayout();
        addChildLayout();
    }

    public void addChildLayout(){

        LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       
        LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
      
        View view=layoutInfralte.inflate(R.layout.activity_main_layout, null);
EditText inflatededitText=(EditText)view.findViewById(R.id.youredittextid);
list.add(inflatededitText);
        
        linearLayout.addView(view);

    }
}

Now you can get any edittext like below

list.get(0).getText.toString

here 0 is position of edittext

if u need all edittext value then use for loop like this

for(int i=0;i<list.length;i++){
 Log.d("value","list.get(i).getText.toString");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM