我想在单击按钮时创建一个低于一个的 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。但是当我尝试将文本输入到编辑文本中时,它会引发以下错误。
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中更改的文本一个没有存储在位置一而是存储在最后一个位置