Android应用开发之多选删除之一
传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
今天我们学习一下如何利用CheckBox实现对联系人记录的多选删除,多选删除功能在平时工作中经常会遇到,通常有两种解决方案:CheckBox实现和ActonBar实现。本人偏爱CheckBox样式的多选模式,因为它实现起来比较灵活多变,样式变换也很容易。而ActionBar的多选样式变换相对而言工作量会大很多。
1案例技术要点(待整理)
2案例代码陈列
工程包目录
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.lynn.multisel" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MultiSelectDeleteActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
strings.xml
<resources> <string name="app_name">CheckBox实现多选删除联系人</string> <string name="delete">删除</string> <string name="cancel">取消</string> <string name="select_all_text">全选</string> <string name="clearConfirmation_title">删除</string> <string name="clearConfirmation">加入的联系人将被删除</string> <string name="clearProgress_title">正在删除…</string> </resources>
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="CheckBoxTheme" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/checkbox_selector</item> </style> <style name="ButtonBarTheme" parent="@android:style/ButtonBar"> <item name="android:background">#006699</item> </style> </resources>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include android:id="@+id/select_all" layout="@layout/select_all" /> <ListView android:id="@+id/del_list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#ffffff" android:cacheColorHint="#00000000" android:listSelector="#00000000" /> <LinearLayout style="@style/ButtonBarTheme" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/del_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:enabled="false" android:text="@string/delete" /> <Button android:id="@+id/can_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/cancel" /> </LinearLayout> </LinearLayout>
list_item.xm
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:descendantFocusability="blocksDescendants" android:gravity="center_vertical" android:orientation="horizontal" android:background="#66ccff" > <CheckBox android:id="@+id/select_chk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:visibility="visible" style="@style/CheckBoxTheme" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/name_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/select_chk" android:layout_marginRight="230dp" android:layout_centerVertical="true" android:singleLine="true" android:textSize="18sp" android:textColor="#ffffff" /> </RelativeLayout>
select_all.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:background="#006699" > <CheckBox android:id="@+id/select_all_chk" style="@style/CheckBoxTheme" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/select_all_text" android:textColor="@android:color/white" android:textSize="20sp" /> </LinearLayout>
checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/> <item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/> <item android:drawable="@drawable/checkbox_normal"/> </selector>