Android:根据用户输入动态创建editText矩阵?

时间:2017-07-27 00:34:13

标签: android android-layout matrix

我是Android开发的新手。我已经注意到了一些类似的问题,但没有一个能够很好地回答我的问题,以至于我可以解决它,或者他们依赖于折旧函数。

到目前为止,我已经创建了我的主要课程。我想打开一个屏幕,用户输入两个数字作为他们想要减少的矩阵的行和列长度

public class MainActivity extends AppCompatActivity {

public int numberOfRows;
public int numberOfColumns;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void setRows(View v3) {
    EditText editTextRow = (EditText) findViewById(R.id.editText2);
    if ("".equals(editTextRow.getText())) {
    }
    else {
        numberOfRows = Integer.parseInt(editTextRow.getText().toString());
    }
}

public void setColumns(View v2) {
    EditText editTextCol = (EditText) findViewById(R.id.editText3);
   if ("".equals(editTextCol.getText())) {
        numberOfRows = Integer.parseInt(editTextCol.getText().toString());
    }
}

public void reduce(View view){
    if(numberOfRows != 0 && numberOfColumns != 0) {
    }
    else {
        Intent intent = new Intent(this, ReduceMatrix.class);
        Bundle bundle = new Bundle();
        bundle.putInt("rows", numberOfRows);
        bundle.putInt("cols", numberOfColumns);
        intent.putExtras(bundle);

        startActivity(intent);
    }
}

 }

我无法想出下一个活动,我想动态创建一个editText矩阵......

public class ReduceMatrix extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reduce_matrix);


    Intent passedIntent = getIntent();
    Bundle extras = passedIntent.getExtras();

    int rowNum = extras.getInt("rows");
    int colNum = extras.getInt("cols");

有人能解释如何去做吗?我在考虑某种for循环,但我对android布局非常困难。我应该使用tableLayout吗?还是gridview?每当我有类似的东西时

    layout = new LinearLayout(this);

它告诉我,我的限定符必须在表达式中?

1 个答案:

答案 0 :(得分:0)

您需要以编程方式添加它。这是我的解决方案:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent passedIntent = getIntent();
    Bundle extras = passedIntent.getExtras();

    EditText[][] editTexts = new EditText[rowNum][colNum];

    GridLayout gridLayout = new GridLayout(this);

    //define how many rows and columns to be used in the layout
    gridLayout.setRowCount(rowNum);
    gridLayout.setColumnCount(colNum);

    for (int i = 0; i < rowNum; i++) {
        for (int j = 0; j < colNum; j++) {
            editTexts[i][j] = new EditText(this);
            setPos(editTexts[i][j], i, j);
            gridLayout.addView(editTexts[i][j]);
        }
    }
    setContentView(gridLayout);
}

//putting the edit text according to row and column index
private void setPos(EditText editText, int row, int column) {
    GridLayout.LayoutParams param =new GridLayout.LayoutParams();
    param.width = 100;
    param.height = 100;
    param.setGravity(Gravity.CENTER);
    param.rowSpec = GridLayout.spec(row);
    param.columnSpec = GridLayout.spec(column);
    editText.setLayoutParams(param);
}

这将使您能够动态添加EditText。