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

时间:2015-09-28 08:15:51

标签: android android-edittext

我是指以编程方式创建的editText。我想从我创建的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;

通过单击按钮可以在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++;
    }

这样看起来就像这样:

任务1: ___________

子任务1:________

子任务2:________

任务2: ___________

子任务1:________

任务3: ___________

假设用户输入了必要的信息并单击了提交按钮。 问题进来了。我不知道如何根据用户创建的editText数量获取所有值。

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
    String subtask1 = task1subtask1.getText().toString(); // It would only get subtask1 of task 1

            }    
    }

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

您可以为“任务”确定特定的通用标记,为“子任务”确定另一个,并在每次创建后,将标记设置为您的编辑文本:

edittext.setTag(KEY,“task_X”)其中X是增量ID。

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

答案 2 :(得分:0)

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

            }    
    }

像这样你可以修改addtask()函数的代码