How To Solve Editext in More Numbers in a Layout And Get Values From That?

后端 未结 3 1169
名媛妹妹
名媛妹妹 2021-01-22 17:17

See this picture:

I am designing a sudoku in android, have done the layout like this, so 81 Editext is there. I

3条回答
  •  深忆病人
    2021-01-22 17:59

    Create edittexts dynamically.

    In every iteration you are rewriting the ed variable, so when loop is finished ed only points to the last EditText instance you created.

    You should store all references to all EditTexts:

    EditText ed;
    List allEds = new ArrayList();
    
    for (int i = 0; i < count; i++) {   
    
        ed = new EditText(Activity2.this);
        allEds.add(ed);
        ed.setBackgroundResource(R.color.blackOpacity);
        ed.setId(id);   
        ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        linear.addView(ed);
    }
    

    Now allEds list hold references to all EditTexts, so you can iterate it and get all the data.

    getting data

     String[] strings = new String[](allEds.size());
    
    for(int i=0; i < allEds.size(); i++){
        string[i] = allEds.get(i).getText().toString();
    }
    

    NOTE:

    This is not results your design there may be some changes.but follow these procedure to get exact results. I think you must you use two for loops to get your edittexts in a gridview.

提交回复
热议问题