2016-10-28 7 views
1

私はアンドロイドアプリをユーザーからの入力を入力とするプロジェクトとして作成しています。行と列の行列のそれは動的に行列の行と列を表示するために動的にテキストボックスを作成しました..今私は、逆数、転置、加算、減算などのようなさらなる計算のためにこれらの値が必要です。値は、ここに私のコードで私はそれが助けをeasily.please使用できるように、2次元配列に格納されている,,と をthankx Bに動的に作成されたeditTextボックスから値を取得し、入力された値を2次元配列に保存する方法

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 
    int rows = getIntent().getIntExtra("rows",3); 
    int cols = getIntent().getIntExtra("cols",3); 
    matrix = (LinearLayout) findViewById(R.id.matrix); 
    matrix.removeAllViews(); 
    List<EditText> allEds = new ArrayList<EditText>();//thisline 


    for (int a = 1; a <= rows; a++) 
    { 
     LinearLayout layout = new LinearLayout(Main2Activity.this); 
     layout.setOrientation(LinearLayout.HORIZONTAL); 
     layout.setLayoutParams(new LinearLayout.LayoutParams 
       (LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1)); 
     for (int b = 1; b <= cols; b++) 
     { 
      EditText text = new EditText(Main2Activity.this); 
      //text.setBackgroundColor(Color.GRAY); 
      allEds.add(text);//thisline 

      text.setHint("**"); 
      text.setKeyListener(new DigitsKeyListener()); 

      text.setHintTextColor(Color.BLACK); 
      int iD = 1; 
      //noinspection ResourceType 
      text.setId(iD); 
      text.setLayoutParams(new LinearLayout.LayoutParams 
        (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1)); 
      //text.setText((j + 1) + " "); 
      text.setTextColor(Color.RED); 
      layout.addView(text); 
     } 
     matrix.addView(layout); 
    } 
    String[] strings = new String[allEds.size()]; 

    for(int a=1; a <= allEds.size();a++) 
    { 
     for (int b = 1; b <= cols; b++) 
     { 
      strings[b] = allEds.get(b).getText().toString(); 

     } 
    } 

答えて

-1
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main2); 
int rows = getIntent().getIntExtra("rows",3); 
int cols = getIntent().getIntExtra("cols",3); 
matrix = (LinearLayout) findViewById(R.id.matrix); 
matrix.removeAllViews(); 

//List<EditText> allEds = new ArrayList<EditText>();//thisline 
//Now you have 2D array of 
EditText editTextMatrix[][] = new EditText[rows][cols]; 


//for (int a = 1; a <= rows; a++) 
for (int a = 0; a < rows; a++) 
{ 
    LinearLayout layout = new LinearLayout(Main2Activity.this); 
    layout.setOrientation(LinearLayout.HORIZONTAL); 
    layout.setLayoutParams(new LinearLayout.LayoutParams 
      (LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1)); 


    //for (int b = 1; b <= cols; b++)   
    for (int b = 0; b < cols; b++) 
    { 
     EditText text = new EditText(Main2Activity.this); 
     //text.setBackgroundColor(Color.GRAY); 
     // allEds.add(text);//thisline 

     editTextMatrix[a][b] = text; 

     text.setHint("**"); 
     text.setKeyListener(new DigitsKeyListener()); 

     text.setHintTextColor(Color.BLACK); 

     //Setting ID to editText is not compulsory as you are saving a reference in editTextMatrix[][] 
     //int iD = 1; 
     //noinspection ResourceType 
     //text.setId(iD); 

     text.setLayoutParams(new LinearLayout.LayoutParams 
       (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1)); 
     //text.setText((j + 1) + " "); 
     text.setTextColor(Color.RED); 
     layout.addView(text); 
    } 
    matrix.addView(layout); 
} 


//Now you have 2D array of editTextMatrix 
//Iterate editTextMatrix to get the values; 
関連する問題