java - 如何从动态创建的 editText 框中获取值并将输入的值保存到二维数组中

标签 java android-studio

我正在制作一个Android应用程序作为一个项目,其中它接受用户的输入作为输入号。矩阵的行和列,然后动态创建文本框,以便在下一个意图中显示矩阵的行和列。现在我需要这些值进行进一步计算,例如逆、转置、加法、减法等。我需要这些b 的值存储在二维数组中,以便我可以轻松使用它。请帮助,谢谢 这是我的代码

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();

        }
    }

最佳答案

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;

关于java - 如何从动态创建的 editText 框中获取值并将输入的值保存到二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40303233/

相关文章:

java - ActionListener/actionPerformed 类中的扫描仪错误

java - 观察和通知

android - 配置特定的 gradle flavor 组合

java - 如何在 Android 上使用 HttpsURLConnection 和 HttpResponseCache 强制缓存?

java - 在 Java HTML 感知组件中使用

java - 组合与减少耦合?

java - 将 xml 添加到 android studio 中的 LinearLayout 项目

android-studio - Android Studio 3.1.1 中的时间和日期选择器在哪里?

java - 意外的顶级异常 - 多个库项目

android-studio - 快捷键 Ctrl + B 在组件树中不起作用,无法在 Android Studio Windows 中转到 XML