单击按钮创建N个Edittext

时间:2013-06-13 12:39:38

标签: android android-edittext textchanged

我想在按钮点击下创建一个下面的Edittext。所以我尝试了以下代码。

    private LinearLayout mLayout;
    private EditText mEditText;
    private Button mButton;
    int k = -1;
    int flag;
    int ss=0;
    ArrayList<String> applnserverinstnos = new ArrayList<String>();
    EditText textView[] = new EditText[100];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mEditText = (EditText) findViewById(R.id.editText);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                k++;

                flag=k;
                final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, 50);
                lparams.setMargins(20, 20, 20, 0);
                textView[flag] = new EditText(AddEditBoxActivity.this);
                textView[flag].setLayoutParams(lparams);
                textView[flag].setId(flag);
                applnserverinstnos.add(flag,Integer.toString(flag));
                textView[flag].addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count)
                        {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) 
                        {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void afterTextChanged(Editable s) 
                        {
                            // TODO Auto-generated method stub
                             String c = s.toString(); // read Content
                             applnserverinstnos.add(flag,c);
                             System.out.println("insno       "+applnserverinstnos.get(flag));
                        }
                    });

                mLayout.addView(textView[flag]);


            }
        });
        TextView textView = new TextView(this);
        Button b2= (Button)findViewById(R.id.button1);
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for(int i = 0;i<=k;i++) 
                {
                System.out.println("applserver   "+i+".."+applnserverinstnos.get(i));
                }
            }
        });

    }

这会创建一个低于一的edittext。我尝试创建3个edittext.But当我尝试将文本输入到编辑文本时,它会抛出以下错误。

06-13 12:44:47.686: E/AndroidRuntime(1665): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 0

请帮我找到它。 谢谢。

修改

添加applnserverinstnos.add(flag,Integer.toString(flag));后,它不会抛出错误,但是在edittext中更改的文本没有存储在第一位而是存储在最后位置

3 个答案:

答案 0 :(得分:2)

最后我找到了非常简单和小巧的解决方案。现在,下面的代码创建了N个edittexts,并从创建的edittexts中获取了值。

 private LinearLayout mLayout;
    private EditText mEditText;
    private Button mButton;
    int k = -1;
    int flag;
    int ss=0;
    ArrayList<String> applnserverinstnos = new ArrayList<String>();
    public static EditText textView[] = new EditText[100];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mEditText = (EditText) findViewById(R.id.editText);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try
                {
                k++;
                flag=k;
                final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, 50);
                lparams.setMargins(20, 20, 20, 0);
                textView[flag] = new EditText(AddEditBoxActivity.this);
                textView[flag].setLayoutParams(lparams);
                textView[flag].setId(flag);

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                mLayout.addView(textView[flag]);


            }
        });

        Button b2= (Button)findViewById(R.id.button1);
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for(int i = 0;i<=k;i++) 
                {
                System.out.println("edit values"+ss+"......"+ textView[i].getText().toString());
                }
            }
        });

    }

希望它可以帮助他人。

答案 1 :(得分:0)

只是我的想法: 我想在你点击edittext后,会调用函数afterTextChanged。目前变量flag为3.但applnserverinstnos的大小仍为0.因此applnserverinstnos.add(flag,c);将导致错误。

答案 2 :(得分:0)

您可以尝试创建自己的TextView自定义对象,该对象附加了特定的观察程序,如下所示。

class MyTextView extends TextView{

    public MyTextView(android.content.Context context) {
        super(context);
    }

    @Override
    public void addTextChangedListener(TextWatcher watcher1) {
        // TODO Auto-generated method stub
        super.addTextChangedListener(watcher);
    }

    TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

}

你可以,当然可以自定义它来添加你自己的文本观察器等。所需要的只是一点点的分析。如果有的话,请告诉我