热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android通讯录字母,Android开发之自定义view实现通讯录列表A~Z字母提示效果【附demo源码下载】...

本文实例讲述了Android开发之自定义view实现通讯录列表A~Z字母提示效果。分享给大家供大家参考,具体如下:开发工具:eclipse

本文实例讲述了Android开发之自定义view实现通讯录列表A~Z字母提示效果。分享给大家供大家参考,具体如下:

开发工具:eclipse

运行环境:htc G9 android2.3.3

话不多说,先看效果图

3c05f897ab7a6e9c0634225c526dcca5.png

5fd22d6886cb1880df9684f90f1f85a0.png

其实左右边的A~Z是一个自定义的View,它直接覆盖在ListView上。

MyLetterListView:

public class MyLetterListView extends View {

OnTouchingLetterChangedListener onTouchingLetterChangedListener;

String[] b = {"#","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"};

int choose = -1;

Paint paint = new Paint();

boolean showBkg = false;

public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

public MyLetterListView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public MyLetterListView(Context context) {

super(context);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

if(showBkg){

canvas.drawColor(Color.parseColor("#40000000"));

}

int height = getHeight();

int width = getWidth();

int singleHeight = height / b.length;

for(int i=0;i

paint.setColor(Color.WHITE);

paint.setTypeface(Typeface.DEFAULT_BOLD);

paint.setAntiAlias(true);

if(i == choose){

paint.setColor(Color.parseColor("#3399ff"));

paint.setFakeBoldText(true);

}

float xPos = width/2 - paint.measureText(b[i])/2;

float yPos = singleHeight * i + singleHeight;

canvas.drawText(b[i], xPos, yPos, paint);

paint.reset();

}

}

@Override

public boolean dispatchTouchEvent(MotionEvent event) {

final int action = event.getAction();

final float y = event.getY();

final int oldChoose = choose;

final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;

final int c = (int) (y/getHeight()*b.length);

switch (action) {

case MotionEvent.ACTION_DOWN:

showBkg = true;

if(oldChoose != c && listener != null){

if(c > 0 && c

listener.onTouchingLetterChanged(b[c]);

choose = c;

invalidate();

}

}

break;

case MotionEvent.ACTION_MOVE:

if(oldChoose != c && listener != null){

if(c > 0 && c

listener.onTouchingLetterChanged(b[c]);

choose = c;

invalidate();

}

}

break;

case MotionEvent.ACTION_UP:

showBkg = false;

choose = -1;

invalidate();

break;

}

return true;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

return super.onTouchEvent(event);

}

public void setOnTouchingLetterChangedListener(

OnTouchingLetterChangedListener onTouchingLetterChangedListener) {

this.OnTouchingLetterChangedListener= onTouchingLetterChangedListener;

}

public interface OnTouchingLetterChangedListener{

public void onTouchingLetterChanged(String s);

}

}

然后我在Activity中OnTouchingLetterChangedListener中监听手指触摸到了哪个字母,然后让列表跳转到对应的位置,

弹出首字母提示框:

private class LetterListViewListener implements OnTouchingLetterChangedListener{

@Override

public void onTouchingLetterChanged(final String s) {

if(alphaIndexer.get(s) != null) {

int position = alphaIndexer.get(s);

personList.setSelection(position);

overlay.setText(sections[position]);

overlay.setVisibility(View.VISIBLE);

handler.removeCallbacks(overlayThread);

//延迟一点五秒后执行,让overlay为不可见

handler.postDelayed(overlayThread, 1500);

}

}

}

延迟一秒让弹出的首字母提示框变为不可见,也就是那个首字母提示框只会显示一秒钟的时间:

//设置overlay不可见

private class OverlayThread implements Runnable {

@Override

public void run() {

overlay.setVisibility(View.GONE);

}

}

还有关于解析汉子的首字母拼音的问题,我这里是查的系统数据库,里面正好有sort_key这一列,比如名字是张三,那么他对应的sort_key就是:ZHANG张SAN三,这样一来就容易多了:

//获得汉语拼音首字母

private String getAlpha(String str) {

if (str == null) {

return "#";

}

if (str.trim().length() == 0) {

return "#";

}

char c = str.trim().substring(0, 1).charAt(0);

// 正则表达式,判断首字母是否是英文字母

Pattern pattern = Pattern.compile("^[A-Za-z]+{1}quot;);

if (pattern.matcher(c + "").matches()) {

return (c + "").toUpperCase();

} else {

return "#";

}

}

如果你的数据不是从联系人表中查的,那可以使用第三方jar包,就是pinyin4j-2.5.0。

activity代码和布局文件比较长,我就不在这里贴了。

附:demo源码点击此处本站下载。

希望本文所述对大家Android程序设计有所帮助。



推荐阅读
author-avatar
龍城灬任少
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有