Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Android中创建的EditText中获取值_Android_Android Edittext - Fatal编程技术网

如何从Android中创建的EditText中获取值

如何从Android中创建的EditText中获取值,android,android-edittext,Android,Android Edittext,我是说编辑文本是通过编程创建的。我想从我创建的编辑文本中获取输入的数据 这些是我到目前为止所做的 变量 private GridLayout gridLayout; //for tasks int rowIndex = 2; int colIndex = 1; int rowIndex2 = 2; int colIndex2 = 0; int i=0; int j=0; //database variables MyDBAdapter dbhandler; 要通过单击按钮在gridlay

我是说编辑文本是通过编程创建的。我想从我创建的编辑文本中获取输入的数据

这些是我到目前为止所做的

变量

private GridLayout gridLayout;

//for tasks
int rowIndex = 2;
int colIndex = 1;
int rowIndex2 = 2;
int colIndex2 = 0;

int i=0;
int j=0;

//database variables
MyDBAdapter dbhandler;
要通过单击按钮在gridlayout中创建editText,请执行以下操作

单击“添加新任务”按钮时:

public void addTask(View view) {
        i++;
        Map<String, Integer> idsMap = new HashMap<String, Integer>();
        String tname = "task" + Integer.toString(i);
        EditText editText = new EditText(this);
        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        param.width = GridLayout.LayoutParams.MATCH_PARENT;
        param.rowSpec = GridLayout.spec(rowIndex);
        param.columnSpec = GridLayout.spec(colIndex);
        editText.setTextColor(Color.BLACK);
        editText.setBackgroundResource(android.R.drawable.edit_text);
        editText.setText(tname);
        editText.setLayoutParams(param);

        TextView textView = new TextView(this);
        GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
        param2.rowSpec = GridLayout.spec(rowIndex2);
        param2.columnSpec = GridLayout.spec(colIndex2);
        textView.setPadding(30, 0, 0, 0);
        textView.setTextColor(Color.BLACK);
        textView.setTypeface(Typeface.DEFAULT_BOLD);

        textView.setLayoutParams(param2);

        if (rowIndex > 1) {
            textView.setText("TASK "+Integer.toString(i)+": ");
            editText.setId(i);
            idsMap.put(tname, i);
        }

        gridLayout.addView(editText);
        gridLayout.addView(textView);
        rowIndex++;
        rowIndex2++;
        this.j = 0;
    }
public void addSubtask(View view) {
        j++;
        Map<String, Integer> idsMap = new HashMap<String, Integer>();
        String taskno = "task" + Integer.toString(i) + "subtask" + Integer.toString(j);
        EditText editText = new EditText(this);
        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        param.width = GridLayout.LayoutParams.MATCH_PARENT;
        param.rowSpec = GridLayout.spec(rowIndex);
        param.columnSpec = GridLayout.spec(colIndex);
        editText.setTextColor(Color.BLACK);
        editText.setBackgroundResource(android.R.drawable.edit_text);
        editText.setText(taskno);
        editText.setLayoutParams(param);

        TextView textView = new TextView(this);
        GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
        param2.rowSpec = GridLayout.spec(rowIndex2);
        param2.columnSpec = GridLayout.spec(colIndex2);
        textView.setPadding(30,0,0,0);
        textView.setTextColor(Color.BLACK);

        textView.setLayoutParams(param2);

        if (rowIndex > 1) {
            textView.setText("Subtask "+Integer.toString(j)+": ");
            editText.setId(j);
            idsMap.put(taskno, j);
        }

        gridLayout.addView(editText);
        gridLayout.addView(textView);
        rowIndex++;
        rowIndex2++;
    }

这种实现是不可用的,因为我不知道用户创建了多少任务和子任务。HELPPPP T_uuuut

每次创建编辑文本用于生成ID时,将该ID存储在HashMap中,或使用getChildAt()循环容器视图,然后,如果是EditText,则获取其值。

也许您可以为创建的每个EditText设置自定义标记

您可以为“任务”确定一个特定的通用标记,为“子任务”确定另一个标记,每次创建后,您都可以将标记设置为edittext,如下所示:

setTag(键,“task_X”),其中X是增量id

之后,您可以枚举主容器的所有子容器并检查标记的子容器

List<EditText> allEdittext = new ArrayList<EditText>();

public void addSubtask(View view) {
            j++;
            Map<String, Integer> idsMap = new HashMap<String, Integer>();
            String taskno = "task" + Integer.toString(i) + "subtask" + Integer.toString(j);
            EditText editText = new EditText(this);
            GridLayout.LayoutParams param = new GridLayout.LayoutParams();
            param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            param.width = GridLayout.LayoutParams.MATCH_PARENT;
            param.rowSpec = GridLayout.spec(rowIndex);
            param.columnSpec = GridLayout.spec(colIndex);
            editText.setTextColor(Color.BLACK);
            editText.setBackgroundResource(android.R.drawable.edit_text);
            editText.setText(taskno);
            editText.setLayoutParams(param);

            TextView textView = new TextView(this);
            GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
            param2.rowSpec = GridLayout.spec(rowIndex2);
            param2.columnSpec = GridLayout.spec(colIndex2);
            textView.setPadding(30,0,0,0);
            textView.setTextColor(Color.BLACK);

            textView.setLayoutParams(param2);

            if (rowIndex > 1) {
                textView.setText("Subtask "+Integer.toString(j)+": ");
                editText.setId(j);
                idsMap.put(taskno, j);
            }

    allEdittext.add(editText);


            gridLayout.addView(editText);
            gridLayout.addView(textView);
            rowIndex++;
            rowIndex2++;
        }

这样,您就可以修改addtask()函数的代码,因为它无法识别View.generateId();它没有这样的类该方法只能从API 17中获得,但这并不重要,您甚至可以避免设置IDi需要设置id来获取它们的值\n不需要,只需循环容器子级,然后检查哪个是edittexti需要它们的id,因为我必须获取它们的值并将其放入数据库中。已经做了。问题是我不知道如何根据用户创建的editText的数量获取所有值。我已经为每个值分配了唯一的ID。但我不知道有多少是由用户创建的。当用户创建edittext时,我如何获得值,您知道,因为您创建的函数被调用,所以您可以计算创建的任务和子任务的数量。但我不知道这是不是正确的解决方案。。。FrankMonza的解决方案似乎是一个很好的选择,因为没有View.generateId()类。它无法识别没有的类:myNewEdittext.generateViewId()?此方法由EditText继承,因为EditText继承了View类。您使用的是什么API?String subtask1=allEdittext.get(i).getText().toString();是的,它应该在循环中,循环大小将是可用编辑文本的数量。不起作用。我必须将任务与子任务联系起来。它应该是二维数组。不止一个。
List<EditText> allEdittext = new ArrayList<EditText>();

public void addSubtask(View view) {
            j++;
            Map<String, Integer> idsMap = new HashMap<String, Integer>();
            String taskno = "task" + Integer.toString(i) + "subtask" + Integer.toString(j);
            EditText editText = new EditText(this);
            GridLayout.LayoutParams param = new GridLayout.LayoutParams();
            param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            param.width = GridLayout.LayoutParams.MATCH_PARENT;
            param.rowSpec = GridLayout.spec(rowIndex);
            param.columnSpec = GridLayout.spec(colIndex);
            editText.setTextColor(Color.BLACK);
            editText.setBackgroundResource(android.R.drawable.edit_text);
            editText.setText(taskno);
            editText.setLayoutParams(param);

            TextView textView = new TextView(this);
            GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
            param2.rowSpec = GridLayout.spec(rowIndex2);
            param2.columnSpec = GridLayout.spec(colIndex2);
            textView.setPadding(30,0,0,0);
            textView.setTextColor(Color.BLACK);

            textView.setLayoutParams(param2);

            if (rowIndex > 1) {
                textView.setText("Subtask "+Integer.toString(j)+": ");
                editText.setId(j);
                idsMap.put(taskno, j);
            }

    allEdittext.add(editText);


            gridLayout.addView(editText);
            gridLayout.addView(textView);
            rowIndex++;
            rowIndex2++;
        }
public void submit(){

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //implementation
                String task1 = task1.getText().toString(); // It would only get task1
//i will be the position
    String subtask1 = allEdittext.get(i).getText().toString();// It would only get subtask1 of task 1

            }    
    }