Android:在activity.java中创建CheckBox [英] Android: create CheckBox in activity.java
本文介绍了Android:在activity.java中创建CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Android应用中创建动态数量的复选框(取决于数组流派"的长度),然后我做到了:
Hi i want to create dynamical number of checkboxes (it depends on the length of array "genre") in an Android app then i did:
CheckBox [] checkbox;
String [] genre={"party","sport","music","café","education"};
@Override
protected void onCreate(Bundle savedInstanceState) {
checkbox=new CheckBox[genre.length];
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
for(int i=0;i<genre.length;i++){
CheckBox c=new CheckBox(this);
c.setText(genre[i]);
checkbox[i]=c;
}
ArrayAdapter<CheckBox>adapter= new ArrayAdapter<CheckBox>(this, android.R.layout.simple_list_item_1,checkbox);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setAdapter(adapter);
}
但是在列表视图中,它显示了内存中复选框的数量(我想),为什么?
but in the listview it shows numbers of the checkboxes in memory(I guess), why?
like : Android.wiget.CheckBox@44f03160
Android.wiget.CheckBox@44f03d60
...
推荐答案
您宁愿使用android.R.simple_list_item_checked
项布局并在列表上启用多项选择:
You'd rather use the android.R.simple_list_item_checked
item layout and enable multiple-choice on the list:
String [] genre={"party","sport","music","café","education"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
通过这种方式,您不必自己手动创建复选框
This way you don't have to create the checkboxes manually by yourself
这篇关于Android:在activity.java中创建CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文