bha*_*982 43
实际上你可以;)这只是用户体验的问题,对吧?
试试这个,(1)列表控制集
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemsCanFocus(false);
Run Code Online (Sandbox Code Playgroud)
(2)将列表项定义为
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:background="@drawable/txt_view_bg" />
Run Code Online (Sandbox Code Playgroud)
这与android.R.layout.simple_list_item_multiple_choice除外
android:background="@drawable/txt_view_bg
(3)并将drawable txt_view_bg.xml定义为
<item android:drawable="@drawable/selected"
android:state_checked="true" />
<item android:drawable="@drawable/not_selected" />
Run Code Online (Sandbox Code Playgroud)
注意: - 处理多项选择的首选方法是在点击项目点击时跟踪您自己的选择,而不是依赖于列表中的状态.
Ram*_*ula 14
第1步:将 addAdapter设置为listview.
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, GENRES));
Run Code Online (Sandbox Code Playgroud)
第2步:为listview设置选择模式.下面第二行代码表示应该检查哪个复选框.
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(2, true);
listView.setOnItemClickListener(this);
private static String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
Run Code Online (Sandbox Code Playgroud)
第3步:在SparseBooleanArray中返回已检查的视图,因此您可以使用下面的代码来获取键或值.下面的示例只是在一个String中显示选定的名称.
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
{
SparseBooleanArray sp=getListView().getCheckedItemPositions();
String str="";
for(int i=0;i<sp.size();i++)
{
str+=GENRES[sp.keyAt(i)]+",";
}
Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
此示例存储您已检查的值并将其显示在Toast中.当您取消选中项目时它会更新http://android-coding.blogspot.ro/2011/09/listview-with-multiple-choice.html