当用户将焦点放在视图上时,我想添加三个相互相邻的editText。 另外我想要当用户点击第一个editText时,在现有的一个下面创建一组新的3个editTexts。
此代码返回错误(nullPointerException)
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
switch(v.getId()){
// 5 is the id of the view
case 5:
main=new RelativeLayout(this);
mainParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
main.setLayoutParams(mainParams);
//mainLayout is a LinearLayout
items[no]=new EditText(this);
rates[no]=new EditText(this);
quants[no]=new EditText(this);
items[no].setHint("Enter item name");
rates[no].setHint("Rate");
quants[no].setHint("Quantity");
items[no].setId(id++);
rates[no].setId(id++);
quants[no].setId(id++);
RelativeLayout.LayoutParams etParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
etParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rParams.addRule(RelativeLayout.ALIGN_RIGHT);
items[no].setLayoutParams(etParams);
rates[no].setLayoutParams(rParams);
quants[no].setLayoutParams(rParams);
mainLayout.addView(main);
no++;
}
答案 0 :(得分:0)
试试这个,
// 5 is the id of the view
case 5:
main=new RelativeLayout(this);
mainParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
main.setLayoutParams(mainParams);
//mainLayout is a LinearLayout
items[no]=new EditText(this);
rates[no]=new EditText(this);
quants[no]=new EditText(this);
RelativeLayout.LayoutParams etParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
etParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rParams.addRule(RelativeLayout.ALIGN_RIGHT);
items[no].setLayoutParams(etParams);
rates[no].setLayoutParams(rParams);
quants[no].setLayoutParams(rParams);
mainLayout.addView(main);
items[no].setHint("Enter item name");
rates[no].setHint("Rate");
quants[no].setHint("Quantity");
items[no].setId(id++);
rates[no].setId(id++);
quants[no].setId(id++);
no++;
}