简体   繁体   中英

Java edittext to xml android

I am trying to do a app with multiple EditText and is wondering if there is any easy way to do that.

For instance to add matrix of EditText from your java code to your activity_main.xml or maby do a for loop which adds them at your specified location.

EditText[][] edittext = new EditText[10][10];
gridView = (GridView) findViewById(R.id.gridview);

for (int i=0;i<9;i++){
    for (int j=0;j<9;j++){
        gridView.addView(edittext[i][j], column X, row Y);
    }
}

There is no easier way to make a "form". Each EditText is a different xml component with different id, with its own attributes.

What you can do is an adapter with listview/recyclerview with EditText the holder.

Here is a working example..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout root = (LinearLayout) findViewById(R.id.master);
    EditText t[][] = new EditText[10][10];
    LinearLayout.LayoutParams dim = new LinearLayout.LayoutParams(LinearLayout.LayoutParams
            .WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    for (int i=0;i<9;i++){
        for (int j=0;j<9;j++){
            t[i][j]=new EditText(this);
            t[i][j].setLayoutParams(dim);
            t[i][j].setHint("Hello World , EditText[" + i + "]" + "[" + j + "]");
            root.addView(t[i][j]);
        }
    }
}

屏幕截图

You can do smth like that:

    ArrayList<EditText> editTexts = new ArrayList<>();
    LinearLayout ll = (LinearLayout) findViewById(R.id.container);
    EditText oneOfEditText;
    for (int i = 0; i < 100; i++){
        oneOfEditText = new EditText(this);
        oneOfEditText.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT                    
        ));
        oneOfEditText.setHint("MyHint");
        oneOfEditText.setId(i);
        ll.addView(oneOfEditText);
        editTexts.add(oneOfEditText);
    }

And then you can access these EditTexts like that:

    for (EditText editText : editTexts){
        Log.d("myLog", editText.getText().toString());
    }

But the best practice for such things is to create them statically in xml, and then you can access them either via static ids, or like that:

    for (int i =0; i < ll.getChildCount(); i++){
        editTexts.add((EditText) ll.getChildAt(i));
    }

I think the proper way implement each item is to use an adapter. In your case, you can use SimpleAdapter or create a custom adapter that extends BaseAdapter and set it using setAdapter(ListAdapter) .

You can check the documentation for GridView here: http://developer.android.com/guide/topics/ui/layout/gridview.html

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