android checkbox多选框实现

下面是一个在Android ListView中实现多选的例子代码:

XML布局资源文件



在Activity中绑定列表并显示:

public class ListViewMultipleChoiceExample extends Activity { private ListView lView; private String lv_items[] = { "Android", "iPhone", "BlackBerry", "AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem", "Us", "UK", "India" }; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); lView = (ListView) findViewById(R.id.ListView01); // Set option as Multiple Choice. So that user can able to select more the one option from list lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, lv_items)); lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); } }

 


效果界面如下:



获取选中的列表项操作:
lView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) { List list = new ArrayList(); SparseBooleanArray a = lView.getCheckedItemPositions(); for(int i = 0; i < lv_items.length ; i++) { if (a.valueAt(i)) { Long val = lView.getAdapter().getItemId(a.keyAt(i)); Log.v(TAG, "index=" + val.toString()); list.add(lView.getAdapter().getItemId((a.keyAt(i)))); } } } }); // End of Listener

你可能感兴趣的:(Android应用与开发篇)