How To Solve Editext in More Numbers in a Layout A

2019-02-26 18:26发布

问题:

See this picture:

I am designing a sudoku in android, have done the layout like this, so 81 Editext is there. I want to get all the values in the Editext so its difficult to declare and mange 81 views in activity. Can you suggest me a method which is effective for processing my problem?

回答1:

I would suggest you to use array of Edittext's like this

EditText text1,text2,text3,text4,text5,text6,text7,text8,text9, text21,text22,text23,text24....text 99;
EditText[] fields={text1,text2,text3,text4,text5,text6,text7,text8,text9, text21,text22,text23,text24....text 99};
//Give your edittext ids in ids array like this
int ids[]={R.id.text1,R.id.text2,R.id.text3.....R.id,text99};
int values[9][9]={};

for(int i=0;i<fiends.length;i++)
{
fields[i]=(EditText)findViewById(ids[i]);
}

To get values use the same way take an array and save those values in array like this. use for loop and get the values from the textfields and save it in the 2d array as i have declared in values array. I suppose you know how to code the rest of the thing you really want to know please comment, I'll post the rest.thank u.. I hope this will help you.



回答2:

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<EditText> allEds = new ArrayList<EditText>();

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.



回答3:

You can try out this way:

public static ArrayList<EditText> m_arr_edit= = new ArrayList<EditText>();
  RelativeLayout p_rl_layout=new RelativeLayout(m_context);
  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  LinearLayout  m_ll_timelayout = new LinearLayout(m_context);
  m_ll_timelayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    for (int i = 0; i < p_intervals; i++)
    {
        EditText m_edit = new EditText(m_context);
        m_edit.setLayoutParams(new LinearLayout.LayoutParams(90, LayoutParams.WRAP_CONTENT));
        m_edit.setText("Set Text");
        m_edit.setId(i);
        m_arr_edit.add(m_btn);
        m_ll_timelayout.addView(m_btn); 
    }
    layoutParams.addRule(RelativeLayout.BELOW);
    p_rl_layout.addView(m_ll_timelayout, layoutParams);

I hope it will help you.