get the value of dynamically generated multiple ed

2019-07-21 05:20发布

问题:

i want the text value set on multiple edittext, onclick of button .Edit text is generated dynamically and i want value of each particular edit text.and store that value of text in a string array. here is the code.

if (type.equalsIgnoreCase("Edittext")) {
                Question = cursor1.getString(cursor1
                        .getColumnIndex(AndroidOpenDbHelper.QUESTIONS));
                String ID = cursor1.getString(cursor1
                        .getColumnIndex(AndroidOpenDbHelper.ID));
                // ll_layout1 = (LinearLayout)
                // findViewById(R.id.linearlayout1);
                tv_edittext = new TextView(this);
                tv_edittext.setTextSize(20.0f);
                tv_edittext.setText(Question);
                ll_layout1.addView(tv_edittext);
                et = new EditText(this);
                et.setOnClickListener(this);
                et.setId(EDITTEXT);
                et.setMinLines(1);
                et.setMaxLines(3);
                // editTextList.add(et);
                ll_layout1.addView(et);
                // editTextList.add(et);

                typelist.add(Question);

            }

this code generate Text and edittext how to start ...? thanks in advanced.

回答1:

Change it to

tv_edittext = new MyTextView(this);

where MyTextView is :

public class MyTextView extends TextView{

    public SampleEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.addTextChangedListener(new MyTextWatcher());
    }

    public SampleEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.addTextChangedListener(new MyTextWatcher());
    }

    public SampleEditText(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.addTextChangedListener(new MyTextWatcher());
    }

}

Use a textwatcher as follows:

public class MyTextWatcher implements TextWatcher {

}

No matter how many textviews you dynamically create, a watcher will be created for it and you can get the text being typed in the onTextChanged method of the textwatcher.