Android address book list, ~ Z letter prompt View

Source: Internet
Author: User

Development Tool: Eclipse running environment: HTC G9 android2.3.3

Let's not talk about it much. Read it first.




In fact, the left and right sides of ~ Z is a custom view that is directly overwritten on the listview.

Myletterlistview:

[Java]
View plaincopy
  1. Public class myletterlistview extends view {
  2. Ontouchingletterchangedlistener;
  3. String [] B = {"#", "A", "B", "C", "D", "E", "F", "g ", "H", "I", "J", "k", "l"
  4. , "M", "n", "O", "P", "Q", "r", "S", "T", "U ", "V", "W", "X", "Y", "Z "};
  5. Int choose =-1;
  6. Paint paint = new paint ();
  7. Boolean showbkg = false;
  8. Public myletterlistview (context, attributeset attrs, int defstyle ){
  9. Super (context, attrs, defstyle );
  10. }
  11. Public myletterlistview (context, attributeset attrs ){
  12. Super (context, attrs );
  13. }
  14. Public myletterlistview (context ){
  15. Super (context );
  16. }
  17. @ Override
  18. Protected void ondraw (canvas ){
  19. Super. ondraw (canvas );
  20. If (showbkg ){
  21. Canvas. drawcolor (color. parsecolor ("#40000000 "));
  22. }
  23. Int Height = getheight ();
  24. Int width = getwidth ();
  25. Int singleheight = height/B. length;
  26. For (INT I = 0; I <B. length; I ++ ){
  27. Paint. setcolor (color. White );
  28. Paint. settypeface (typeface. default_bold );
  29. Paint. setantialias (true );
  30. If (I = choose ){
  31. Paint. setcolor (color. parsecolor ("# 3399ff "));
  32. Paint. setfakeboldtext (true );
  33. }
  34. Float xpos = width/2-paint. measuretext (B [I])/2;
  35. Float ypos = singleheight * I + singleheight;
  36. Canvas. drawtext (B [I], xpos, ypos, paint );
  37. Paint. Reset ();
  38. }
  39. }
  40. @ Override
  41. Public Boolean dispatchtouchevent (motionevent event ){
  42. Final int action = event. getaction ();
  43. Final float y = event. Gety ();
  44. Final int oldchoose = choose;
  45. Final ontouchingletterchangedlistener listener = ontouchingletterchangedlistener;
  46. Final int c = (INT) (y/getheight () * B. Length );
  47. Switch (Action ){
  48. Case motionevent. action_down:
  49. Showbkg = true;
  50. If (oldchoose! = C & listener! = NULL ){
  51. If (C> 0 & C <B. Length ){
  52. Listener. ontouchingletterchanged (B [c]);
  53. Choose = C;
  54. Invalidate ();
  55. }
  56. }
  57. Break;
  58. Case motionevent. action_move:
  59. If (oldchoose! = C & listener! = NULL ){
  60. If (C> 0 & C <B. Length ){
  61. Listener. ontouchingletterchanged (B [c]);
  62. Choose = C;
  63. Invalidate ();
  64. }
  65. }
  66. Break;
  67. Case motionevent. action_up:
  68. Showbkg = false;
  69. Choose =-1;
  70. Invalidate ();
  71. Break;
  72. }
  73. Return true;
  74. }
  75. @ Override
  76. Public Boolean ontouchevent (motionevent event ){
  77. Return super. ontouchevent (event );
  78. }
  79. Public void setontouchingletterchangedlistener (
  80. Ontouchingletterchangedlistener ){
  81. This. ontouchingletterchangedlistener = ontouchingletterchangedlistener;
  82. }
  83. Public interface ontouchingletterchangedlistener {
  84. Public void ontouchingletterchanged (string S );
  85. }
  86. }

Then, I listened to the letter that the finger touched in the ontouchingletterchangedlistener of the activity and asked the list to jump to the corresponding position,

The initial letter prompt box is displayed:

[Java]
View plaincopy
  1. Private class letterlistviewlistener implements ontouchingletterchangedlistener {
  2. @ Override
  3. Public void ontouchingletterchanged (final string s ){
  4. If (alphaindexer. Get (s )! = NULL ){
  5. Int position = alphaindexer. Get (s );
  6. Personlist. setselection (position );
  7. Overlay. settext (sections [position]);
  8. Overlay. setvisibility (view. Visible );
  9. Handler. removecallbacks (overlaythread );
  10. // Wait 1.5 seconds to make overlay invisible
  11. Handler. postdelayed (overlaythread, 1500 );
  12. }
  13. }
  14. }

One second delay makes the pop-up initial prompt box invisible, that is, the initial prompt box will only display for one second:

[Java]
View plaincopy
  1. // Set overlay to be invisible
  2. Private class overlaythread implements runnable {
  3. @ Override
  4. Public void run (){
  5. Overlay. setvisibility (view. Gone );
  6. }
  7. }

There is also a question about parsing man's initials in pinyin. I am checking the system database here. There is a sort_key column in it. For example, if the name is James, then the corresponding sort_key is: zhang zhangsan 3 makes it much easier:

[Java]
View plaincopy
  1. // Obtain the first letter of Chinese pinyin
  2. Private string getalpha (string Str ){
  3. If (STR = NULL ){
  4. Return "#";
  5. }
  6. If (Str. Trim (). Length () = 0 ){
  7. Return "#";
  8. }
  9. Char c = Str. Trim (). substring (0, 1). charat (0 );
  10. // Regular expression to determine whether the first letter is an English letter
  11. Pattern pattern = pattern. Compile ("^ [A-Za-Z] + {1} quot ;);
  12. If (pattern. matcher (C + ""). Matches ()){
  13. Return (C + ""). touppercase ();
  14. } Else {
  15. Return "#";
  16. }
  17. }

If your data is not queried from the contact table, you can use a third-party jar package, that is, the pinyin4j-2.5.0.
The activity code and layout file are long and I will not paste them here.

I uploaded a project. If you are interested, download it and check it out. I hope you will not be able to write well enough.

Source code (0 resource points): http://download.csdn.net/detail/luck_apple/3629137

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.