【笔记】通讯录使用侧栏字母索引
来源:互联网 发布:js判断元素显示隐藏 编辑:程序博客网 时间:2024/05/21 17:41
侧边字母索引在通讯录、地区选择之类的长listview中比较实用。
自定义View
public class SectionIndexBar extends View { //索引 private String[] indexs = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; private int selectIndex = -1; private Paint paintNormal; private Paint paintSelect; //普通色 private int colorNormal = Color.WHITE; //选中色 private int colorSelect = Color.BLACK; //字体大小 private float textSize = 40f; private int mWidth; private int itemHeight; //背景色 private int colorBackground = Color.GRAY; private OnIndexListener onIndexListener; public void setOnIndexListener(OnIndexListener onIndexListener) { this.onIndexListener = onIndexListener; } public interface OnIndexListener { void onIndexSelect(String str); void onIndexChange(String str); } public SectionIndexBar(Context context) { this(context, null); } public SectionIndexBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { setClickable(true); paintNormal = new Paint(); paintNormal.setColor(colorNormal); paintNormal.setAntiAlias(true); paintNormal.setTextSize(textSize); paintSelect = new Paint(); paintSelect.setColor(colorSelect); paintNormal.setAntiAlias(true); paintSelect.setTextSize(textSize); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //设置背景 setBackgroundColor(colorBackground); mWidth = getWidth(); itemHeight = getHeight() / indexs.length; drawChars(canvas); invalidate(); } /** * 绘制字母 * * @param canvas */ private void drawChars(Canvas canvas) { for (int i = 0; i < indexs.length; i++) { if (selectIndex == i) { //绘制选中字母 canvas.drawText(indexs[i], (mWidth - paintNormal.measureText(indexs[i])) / 2, (i + 1) * itemHeight, paintSelect); } else { //绘制普通字母 canvas.drawText(indexs[i], (mWidth - paintSelect.measureText(indexs[i])) / 2, (i + 1) * itemHeight, paintNormal); } } } /** * 手势控制 * @param event * @return */ @Override public boolean onTouchEvent(MotionEvent event) { int count = (int) event.getY() / itemHeight; count = count < 0 ? 0 : (count >= indexs.length ? indexs.length - 1 : count); selectIndex = count; if (onIndexListener != null) { onIndexListener.onIndexChange(indexs[selectIndex]); } if (event.getAction() == MotionEvent.ACTION_UP) { if (onIndexListener != null) { onIndexListener.onIndexSelect(indexs[selectIndex]); } } return super.onTouchEvent(event); }}
获取联系人
public class ContactUtil { private static final String[] PHONES_PROJECTION = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; /** * 联系人显示名称 **/ private static final int PHONES_DISPLAY_NAME_INDEX = 0; /** * 电话号码 **/ private static final int PHONES_NUMBER_INDEX = 1; /** * 得到所有的手机号码联系人信息 **/ public static List<PhoneContact> getAllPhoneContacts(Context context) { List<PhoneContact> phoneContactList = getPhoneContact(context); List<PhoneContact> simContactList = getSIMContacts(context); for (PhoneContact contact : simContactList) { if (!isContainObject(phoneContactList, contact)) { phoneContactList.add(contact); } } return phoneContactList; } /* * 某个联系人是否已经存在于联系人列表中 */ public static boolean isContainObject(List<PhoneContact> phoneContactList, PhoneContact contact) { for (int i = 0; i < phoneContactList.size(); i++) { PhoneContact curContact = phoneContactList.get(i); if (curContact.getMobile().equals(contact.getMobile()) && curContact.getName().equals(contact.getName())) { return true; } } return false; } /** * 获取手机通讯录联系人 * * @param context * @return */ public static List<PhoneContact> getPhoneContact(Context context) { List<PhoneContact> list = new ArrayList<PhoneContact>(); try { ContentResolver contentResolver = context.getContentResolver(); Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; Cursor cursor = contentResolver.query(contentUri, PHONES_PROJECTION, null, null, null); while (cursor != null ? cursor.moveToNext() : false) { String name = cursor.getString(0); String mobile = cursor.getString(1).replace("+86", "").replace(" ", "").replace("-", ""); String namePinYin = PinYinUtil.getPinYin(name).toUpperCase(Locale.US); PhoneContact contactInfo = new PhoneContact(name, mobile, namePinYin); list.add(contactInfo); } cursor.close(); } catch (Exception e) { } return list; } /** * 得到手机SIM卡联系人人信息 **/ private static List<PhoneContact> getSIMContacts(Context context) { List<PhoneContact> list = new ArrayList<PhoneContact>(); try { ContentResolver contentResolver = context.getContentResolver(); Uri uri = Uri.parse("content://icc/adn"); Cursor cursor = contentResolver.query(uri, PHONES_PROJECTION, null, null, null); while (cursor != null ? cursor.moveToNext() : false) { String name = cursor.getString(PHONES_DISPLAY_NAME_INDEX); String mobile = cursor.getString(PHONES_NUMBER_INDEX).replace("+86", "").replace(" ", "").replace("-", ""); String namePinYin = PinYinUtil.getPinYin(name).toUpperCase(Locale.US); PhoneContact contactInfo = new PhoneContact(name, mobile, namePinYin); list.add(contactInfo); } cursor.close(); } catch (Exception ex) { } return list; }}
源码
0 0
- 【笔记】通讯录使用侧栏字母索引
- 自定义通讯录字母索引
- android仿微信通讯录字母索引控件
- Android ListView字母索引(仿微信通讯录列表)
- Android实现通讯录功能,排序+字母索引
- Android 自定义 View 实现通讯录字母索引(仿微信通讯录)
- Android 自定义 View 实现通讯录字母索引(仿微信通讯录)
- Andorid使用自定义View实现通讯录,媒体文件等等的ListView的字母索引
- Android获取通讯录联系人,右侧字母滑动索引
- Android仿微信通讯录功能,好友排序+字母索引
- Android获取通讯录联系人,右侧字母滑动索引
- Android仿微信通讯录功能,好友排序+字母索引
- Android学习笔记(三)之带有侧边索引的快速查找(跟带字母索引查找的通讯录差不多)
- Android学习笔记(三)之带有侧边索引的快速查找(跟带字母索引查找的通讯录差不多)
- Android学习笔记(三)之带有侧边索引的快速查找(跟带字母索引查找的通讯录差不多)
- Android listview 侧栏字母索引排序
- 通讯录侧滑索引菜单
- 仿微信通讯录如何得到侧栏索引的搜索字符
- activity的生命周期
- android autolayout适配
- 写入字符到文件:java
- 不可靠信号问题
- bzoj 1601: [Usaco2008 Oct]灌水
- 【笔记】通讯录使用侧栏字母索引
- leetcode_350(求两个数组的交集)
- [Java面试九]脚本语言知识总结.
- git基础问题汇总
- C语言-统计二进制数中1的个数-Hamming weight-汉明权重问题
- 错误:HttpServlet was not found on the Java
- [Java面试十]浏览器跨域问题.
- eclipse创建maven工程出错了
- ffmpeg之AVInputFormat结构体