手机通讯录博文2



注:博文1是总体运行效果图,此文对各个部分分开解释。


然后我在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);  
            }   
       }  
         
    } 

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



 	private class OverlayThread implements Runnable {  
        @Override  
        public void run() {  
            overlay.setVisibility(View.GONE);  
        }    
    }

 

 3   还有关于解析汉子的首字母拼音的问题,我这里是查的系统数据库,里面正好有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 "#";    
        }    
}  

4 首字母提示的view,AlphaView:

public class AlphaView extends ImageView {
  private Drawable alphaDrawable;
  private boolean showBkg; 
 private int choose; // 当前选中首字母的位置
 private String[] ALPHAS;
  private OnAlphaChangedListener listener;

 public AlphaView(Context context) {
   super(context);
   initAlphaView();
  }

 public AlphaView(Context context, AttributeSet attrs) {
   super(context, attrs);
   initAlphaView();
  }

 public AlphaView(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   initAlphaView();
  }

 private void initAlphaView() {
   showBkg = false;
   choose = -1;
   setImageResource(R.drawable.alpha_normal);
   alphaDrawable = getDrawable();
   
   ALPHAS = new String[28];
   ALPHAS[0] = " "; // " "代表搜索
  ALPHAS[27] = "#";
   for (int i = 0; i < 26; i++) {
    ALPHAS[i + 1] = String.valueOf((char) (65 + i));
   }
  }

 @Override
  protected void onDraw(Canvas canvas) {
   if (showBkg) {
    setImageResource(R.drawable.alpha_pressed);
    alphaDrawable = getDrawable();

   alphaDrawable.setBounds(0, 0, getWidth(), getHeight());
   } else {
    setImageResource(R.drawable.alpha_normal);
    alphaDrawable = getDrawable();

   alphaDrawable.setBounds(0, 0, getWidth(), getHeight());
   }

  canvas.save();
   alphaDrawable.draw(canvas);
   canvas.restore();
  }

 @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
   final float y = event.getY();
   final int oldChoose = choose;
   final int c = (int) (y / getHeight() * 28);

  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    showBkg = true;
    if (oldChoose != c && listener != null) {
     if (c >= 0 && c < ALPHAS.length) {
      listener.OnAlphaChanged(ALPHAS[c], c);
      choose = c;
     }
    }
    invalidate();
    break;

  case MotionEvent.ACTION_MOVE:
    if (oldChoose != c && listener != null) {
     if (c >= 0 && c < ALPHAS.length) {
      listener.OnAlphaChanged(ALPHAS[c], c);
      choose = c;
     }
    }
    invalidate();
    break;

  case MotionEvent.ACTION_UP:
    showBkg = false;
    choose = -1;
    invalidate();
    break;
   }
   return true;
  }

 // 设置事件
 public void setOnAlphaChangedListener(OnAlphaChangedListener listener) {
   this.listener = listener;
  }

 // 事件接口
 public interface OnAlphaChangedListener {
   public void OnAlphaChanged(String s, int index);
  }
}


  小结

实现了一个基本的手机通讯录软件。主要内容包括分组的添加、删除、重命名、联系人的添加、删除、各种数据的显示、模糊查询、通话以及短信,数据文件的存储和读取。  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值