我需要在Android中动态创建的EditText领域。我已经通过链接和写按钮点击操作它。这是当我点击按钮,复选框有显示。但是,当我创建的复选框对象的onclick行动它显示错误。
有人能告诉我为什么会显示错误?
我的code:
公共类InflationActivity延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
滚动型SV =新的滚动型(本);
最后的LinearLayout LL =新的LinearLayout(本);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(Ⅱ);
TextView的电视=新的TextView(本);
tv.setText(FTW动态布局!);
ll.addView(电视);
等的EditText =新的EditText(本);
et.setText(weeeeeeeeeee〜!);
ll.addView(等);
按钮B =新的按钮(这一点);
b.setText(我没有做任何事情,但我是动态地添加:)。);
ll.addView(B);
b.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
的for(int i = 0;我小于20;我++){
复选框CH =新的复选框(本);
ch.setText(我动!);
ll.addView(CH);
}
}
});
this.setContentView(SV);
}
}
解决方案
只要你的听众变为(完美的工作,我都试过):
b.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
的for(int i = 0;我小于20;我++){
复选框CB =新的复选框(getApplicationContext());
cb.setText(我动!);
ll.addView(CB);
}
}
});
请注意两个变化:
View.OnClickListener
到 OnClickListener
新的复选框(这一点)
到新的复选框(getApplicationContext())
I need to create edittext fields dynamically in android. I have gone through the link and had written the button click action for it. That is when I click on the button the check boxes has to display. But when I am creating the checkbox object in the onclick action it is showing error.
Can someone please tell me why is it showing error?
My code :
public class InflationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
EditText et = new EditText(this);
et.setText("weeeeeeeeeee~!");
ll.addView(et);
Button b = new Button(this);
b.setText("I don't do anything, but I was added dynamically. :)");
ll.addView(b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i < 20; i++) {
CheckBox ch = new CheckBox(this);
ch.setText("I'm dynamic!");
ll.addView(ch);
}
}
});
this.setContentView(sv);
}
}
解决方案
Just change your listener to(perfectly working,I have tried):
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
}
});
Note the two changes:
View.OnClickListener
to OnClickListener
new CheckBox(this)
to new CheckBox(getApplicationContext())
上一篇:MyLocationOverlay disableMyLocation()似乎不工作工作、MyLocationOverlay、disableMyLocation
下一篇:&QUOT;而自动启动捆绑com.android.ide.eclipse.adt&QUOT发生错误;发生错误、自动启动、com、QUOT