本文實例講述了Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)是專業(yè)的渾江網(wǎng)站建設公司,渾江接單;提供成都網(wǎng)站制作、網(wǎng)站建設,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行渾江網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
開發(fā)工具:eclipse
運行環(huán)境:htc G9 android2.3.3
話不多說,先看效果圖
其實左右邊的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;i0 && c< b.length){ listener.onTouchingLetterChanged(b[c]); choose = c; invalidate(); } } break; case MotionEvent.ACTION_MOVE: if(oldChoose != c && listener != null){ if(c > 0 && c< b.length){ 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中監(jiān)聽手指觸摸到了哪個字母,然后讓列表跳轉(zhuǎn)到對應的位置,
彈出首字母提示框:
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); //延遲一點五秒后執(zhí)行,讓overlay為不可見 handler.postDelayed(overlayThread, 1500); } } }
延遲一秒讓彈出的首字母提示框變?yōu)椴豢梢姡簿褪悄莻€首字母提示框只會顯示一秒鐘的時間:
//設置overlay不可見 private class OverlayThread implements Runnable { @Override public void run() { overlay.setVisibility(View.GONE); } }
還有關于解析漢子的首字母拼音的問題,我這里是查的系統(tǒng)數(shù)據(jù)庫,里面正好有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 "#"; } }
如果你的數(shù)據(jù)不是從聯(lián)系人表中查的,那可以使用第三方jar包,就是pinyin4j-2.5.0。
activity代碼和布局文件比較長,我就不在這里貼了。
附:demo源碼點擊此處本站下載。
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。