Android实战简易教程<六十>(分享一个城市选择功能模块)

在做一些APP的时候可能会用到城市选择模块的功能,本实例可以提供位置自定定位和手动选择的功能,非常好用,大家可以修改后加入自己的项目中。

首先看一下效果:

1.进入有的手机可能会提示权限要求,这时我们选择允许。


2.选择允许后进入:


这个位置可以看到我们的城市已经通过GPS获取到了


点击这个控件,城市获取完成了:


下面我们看一下关键代码如何实现这个功能效果。

1.MainActivity.java:

[java]  view plain copy
  1. package com.winxiang.locationselect;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.TextView;  
  8.   
  9. public class MainActivity extends Activity {  
  10.     private TextView city_name;  
  11.       
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         city_name = (TextView) findViewById(R.id.city_name);  
  17.     }  
  18.       
  19.     public void goSelcet(View v){  
  20.         startActivityForResult(new Intent(MainActivity.this,ActivitySelectCity.class), 99);  
  21.     }  
  22.     @Override  
  23.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  24.         // TODO Auto-generated method stub  
  25.         try{  
  26.             switch (resultCode) {  
  27.             case 99:  
  28.                 city_name.setText(data.getStringExtra("lngCityName"));  
  29.                 break;  
  30.       
  31.             default:  
  32.                 break;  
  33.             }  
  34.         }catch (Exception e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.         super.onActivityResult(requestCode, resultCode, data);  
  38.     }  
  39. }  
很是简单,有一个跳转按钮。

2.ActivitySelectCity.java:

[java]  view plain copy
  1. package com.winxiang.locationselect;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Comparator;  
  6. import java.util.HashMap;  
  7. import java.util.List;  
  8. import java.util.regex.Pattern;  
  9.   
  10. import org.json.JSONArray;  
  11. import org.json.JSONObject;  
  12.   
  13. import com.baidu.location.BDLocation;  
  14. import com.baidu.location.BDLocationListener;  
  15. import com.baidu.location.LocationClient;  
  16. import com.baidu.location.LocationClientOption;  
  17.   
  18. import android.app.Activity;  
  19. import android.app.ProgressDialog;  
  20. import android.content.Context;  
  21. import android.content.Intent;  
  22. import android.graphics.PixelFormat;  
  23. import android.os.Bundle;  
  24. import android.os.Handler;  
  25. import android.text.Editable;  
  26. import android.text.TextWatcher;  
  27. import android.view.LayoutInflater;  
  28. import android.view.View;  
  29. import android.view.View.OnClickListener;  
  30. import android.view.ViewGroup;  
  31. import android.view.ViewGroup.LayoutParams;  
  32. import android.view.WindowManager;  
  33. import android.widget.AdapterView;  
  34. import android.widget.AdapterView.OnItemClickListener;  
  35. import android.widget.BaseAdapter;  
  36. import android.widget.EditText;  
  37. import android.widget.ImageView;  
  38. import android.widget.LinearLayout;  
  39. import android.widget.ListView;  
  40. import android.widget.TextView;  
  41.   
  42. import com.winxiang.locationselect.MyLetterListView.OnTouchingLetterChangedListener;  
  43.   
  44. public class ActivitySelectCity extends Activity{  
  45.     private ListAdapter adapter;  
  46.     private ListView personList;  
  47.     private ImageView imgback;  
  48.     private TextView overlay; // 对话框首字母textview  
  49.     private MyLetterListView letterListView; // A-Z listview  
  50.     private HashMap<String, Integer> alphaIndexer;// 存放存在的汉语拼音首字母和与之对应的列表位置  
  51.     private String[] sections;// 存放存在的汉语拼音首字母  
  52.     private Handler handler;  
  53.     private OverlayThread overlayThread; // 显示首字母对话框  
  54.     private ArrayList<City> allCity_lists; // 所有城市列表  
  55.     private ArrayList<City> ShowCity_lists; // 需要显示的城市列表-随搜索而改变  
  56.     private ArrayList<City> city_lists;// 城市列表  
  57.     private String lngCityName ="";//存放返回的城市名  
  58.     private JSONArray chineseCities ;  
  59.     private LocationClient locationClient = null;  
  60.     private EditText sh;  
  61.     private TextView lng_city;  
  62.     private LinearLayout lng_city_lay;  
  63.     private ProgressDialog progress;  
  64.     private static final int SHOWDIALOG = 2;  
  65.     private static final int DISMISSDIALOG = 3;  
  66.   
  67.     @Override  
  68.     public void onCreate(Bundle savedInstanceState) {  
  69.         super.onCreate(savedInstanceState);  
  70.         setContentView(R.layout.activity_selectcity);  
  71.         personList = (ListView) findViewById(R.id.list_view);  
  72.         allCity_lists = new ArrayList<City>();  
  73.         letterListView = (MyLetterListView) findViewById(R.id.MyLetterListView01);  
  74.         lng_city_lay = (LinearLayout) findViewById(R.id.lng_city_lay);  
  75.         sh = (EditText) findViewById(R.id.sh);  
  76.         lng_city = (TextView) findViewById(R.id.lng_city);  
  77.         imgback = (ImageView) findViewById(R.id.imgback);  
  78.           
  79.         letterListView.setOnTouchingLetterChangedListener(new LetterListViewListener());  
  80.         alphaIndexer = new HashMap<String, Integer>();  
  81.         handler = new Handler();  
  82.         overlayThread = new OverlayThread();  
  83.         personList.setOnItemClickListener(new OnItemClickListener() {  
  84.             @Override  
  85.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  86.                     long arg3) {  
  87.                 Intent intent = new Intent();  
  88.                 intent.putExtra("lngCityName", ShowCity_lists.get(arg2).name);  
  89.                 setResult(99,intent);  
  90.                 finish();  
  91.             }  
  92.         });  
  93.         lng_city_lay.setOnClickListener(new OnClickListener() {  
  94.               
  95.             @Override  
  96.             public void onClick(View v) {  
  97.                 Intent intent = new Intent();  
  98.                 intent.putExtra("lngCityName",lngCityName);  
  99.                 setResult(99,intent);  
  100.                 finish();  
  101.             }  
  102.         });  
  103.         imgback.setOnClickListener(new OnClickListener() {  
  104.               
  105.             @Override  
  106.             public void onClick(View v) {  
  107.                 finish();  
  108.             }  
  109.         });  
  110.           
  111.         initGps();  
  112.         initOverlay();  
  113.         handler2.sendEmptyMessage(SHOWDIALOG);  
  114.         Thread thread = new Thread(){  
  115.             @Override  
  116.             public void run() {  
  117.                 hotCityInit();  
  118.                 handler2.sendEmptyMessage(DISMISSDIALOG);  
  119.                 super.run();  
  120.             }  
  121.         };  
  122.         thread.start();  
  123.     }  
  124.   
  125.     /** 
  126.      * 热门城市 
  127.      */  
  128.     public void hotCityInit() {  
  129.         City city;     
  130.         city = new City("上海""");  
  131.         allCity_lists.add(city);  
  132.         city = new City("北京""");  
  133.         allCity_lists.add(city);  
  134.         city = new City("广州""");  
  135.         allCity_lists.add(city);  
  136.         city = new City("深圳""");  
  137.         allCity_lists.add(city);  
  138.         city = new City("武汉""");  
  139.         allCity_lists.add(city);  
  140.         city = new City("天津""");  
  141.         allCity_lists.add(city);  
  142.         city = new City("西安""");  
  143.         allCity_lists.add(city);  
  144.         city = new City("南京""");  
  145.         allCity_lists.add(city);  
  146.         city = new City("杭州""");  
  147.         allCity_lists.add(city);  
  148.         city = new City("成都""");  
  149.         allCity_lists.add(city);  
  150.         city = new City("重庆""");  
  151.         allCity_lists.add(city);  
  152.         city_lists = getCityList();  
  153.         allCity_lists.addAll(city_lists);  
  154.         ShowCity_lists=allCity_lists;  
  155.     }  
  156.   
  157.     /** 
  158.      *  
  159.      * 通过json数据获取城市列表 
  160.      * @author yayun 
  161.      * @since 2015年9月18日 
  162.      *@return 
  163.      */  
  164.     private ArrayList<City> getCityList() {  
  165.         ArrayList<City> list = new ArrayList<City>();  
  166.         try {  
  167.             chineseCities = new JSONArray(getResources().getString(R.string.citys));  
  168.             for(int i=0;i<chineseCities.length();i++){  
  169.                 JSONObject jsonObject = chineseCities.getJSONObject(i);  
  170.                 City city = new City(jsonObject.getString("name"), jsonObject.getString("pinyin"));  
  171.                 list.add(city);  
  172.             }  
  173.               
  174.         } catch (Exception e) {  
  175.             e.printStackTrace();  
  176.         }  
  177.         Collections.sort(list, comparator);  
  178.         return list;  
  179.     }  
  180.   
  181.     /** 
  182.      * a-z排序 
  183.      */  
  184.     Comparator comparator = new Comparator<City>() {  
  185.         @Override  
  186.         public int compare(City lhs, City rhs) {  
  187.             String a = lhs.getPinyi().substring(01);  
  188.             String b = rhs.getPinyi().substring(01);  
  189.             int flag = a.compareTo(b);  
  190.             if (flag == 0) {  
  191.                 return a.compareTo(b);  
  192.             } else {  
  193.                 return flag;  
  194.             }  
  195.   
  196.         }  
  197.     };  
  198.   
  199.   
  200.     /** 
  201.      * ListView的adapter 
  202.      * @author yayun 
  203.      * @since 2015年9月18日 
  204.      * 
  205.      */  
  206.     public class ListAdapter extends BaseAdapter {  
  207.         private LayoutInflater inflater;  
  208.         final int VIEW_TYPE = 3;  
  209.   
  210.         public ListAdapter(Context context) {  
  211.             this.inflater = LayoutInflater.from(context);  
  212.             alphaIndexer = new HashMap<String, Integer>();  
  213.             sections = new String[ShowCity_lists.size()];  
  214.             for (int i = 0; i < ShowCity_lists.size(); i++) {  
  215.                 // 当前汉语拼音首字母  
  216.                 String currentStr = getAlpha(ShowCity_lists.get(i).getPinyi());  
  217.                 // 上一个汉语拼音首字母,如果不存在为“ ”  
  218.                 String previewStr = (i - 1) >= 0 ? getAlpha(ShowCity_lists.get(i - 1)  
  219.                         .getPinyi()) : " ";  
  220.                 if (!previewStr.equals(currentStr)) {  
  221.                     String name = getAlpha(ShowCity_lists.get(i).getPinyi());  
  222.                     alphaIndexer.put(name, i);  
  223.                     sections[i] = name;  
  224.                 }  
  225.             }  
  226.         }  
  227.   
  228.         @Override  
  229.         public int getCount() {  
  230.             return ShowCity_lists.size();  
  231.         }  
  232.   
  233.         @Override  
  234.         public Object getItem(int position) {  
  235.             return ShowCity_lists.get(position);  
  236.         }  
  237.   
  238.         @Override  
  239.         public long getItemId(int position) {  
  240.             return position;  
  241.         }  
  242.   
  243.         @Override  
  244.         public int getItemViewType(int position) {  
  245.             // TODO Auto-generated method stub  
  246.             int type = 2;  
  247.               
  248.             if (position == 0&&sh.getText().length()==0) {//不是在搜索状态下  
  249.                 type = 0;  
  250.             }  
  251.             return type;  
  252.         }  
  253.   
  254.         @Override  
  255.         public int getViewTypeCount() {// 这里需要返回需要集中布局类型,总大小为类型的种数的下标  
  256.             return VIEW_TYPE;  
  257.         }  
  258.   
  259.         @Override  
  260.         public View getView(int position, View convertView, ViewGroup parent) {  
  261.             ViewHolder holder;  
  262.             int viewType = getItemViewType(position);  
  263.                 if (convertView == null) {  
  264.                     convertView = inflater.inflate(R.layout.list_item, null);  
  265.                     holder = new ViewHolder();  
  266.                     holder.alpha = (TextView) convertView  
  267.                             .findViewById(R.id.alpha);  
  268.                     holder.name = (TextView) convertView  
  269.                             .findViewById(R.id.name);  
  270.                     convertView.setTag(holder);  
  271.                 } else {  
  272.                     holder = (ViewHolder) convertView.getTag();  
  273.                 }  
  274. //              if (sh.getText().length()==0) {//搜所状态  
  275. //                  holder.name.setText(list.get(position).getName());  
  276. //                  holder.alpha.setVisibility(View.GONE);  
  277. //              }else if(position>0){  
  278.                 //显示拼音和热门城市,一次检查本次拼音和上一个字的拼音,如果一样则不显示,如果不一样则显示  
  279.                   
  280.                     holder.name.setText(ShowCity_lists.get(position).getName());  
  281.                     String currentStr = getAlpha(ShowCity_lists.get(position).getPinyi());//本次拼音  
  282.                     String previewStr = (position-1) >= 0 ? getAlpha(ShowCity_lists.get(position-1).getPinyi()) : " ";//上一个拼音  
  283.                     if (!previewStr.equals(currentStr)) {//不一样则显示  
  284.                         holder.alpha.setVisibility(View.VISIBLE);  
  285.                         if (currentStr.equals("#")) {  
  286.                             currentStr = "热门城市";  
  287.                         }  
  288.                         holder.alpha.setText(currentStr);  
  289.                     } else {  
  290.                         holder.alpha.setVisibility(View.GONE);  
  291.                     }  
  292. //              }  
  293.             return convertView;  
  294.         }  
  295.   
  296.         private class ViewHolder {  
  297.             TextView alpha; // 首字母标题  
  298.             TextView name; // 城市名字  
  299.         }  
  300.     }  
  301.   
  302.     // 初始化汉语拼音首字母弹出提示框  
  303.     private void initOverlay() {  
  304.         LayoutInflater inflater = LayoutInflater.from(this);  
  305.         overlay = (TextView) inflater.inflate(R.layout.overlay, null);  
  306.         overlay.setVisibility(View.INVISIBLE);  
  307.         WindowManager.LayoutParams lp = new WindowManager.LayoutParams(  
  308.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,  
  309.                 WindowManager.LayoutParams.TYPE_APPLICATION,  
  310.                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  
  311.                         | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,  
  312.                 PixelFormat.TRANSLUCENT);  
  313.         WindowManager windowManager = (WindowManager) this  
  314.                 .getSystemService(Context.WINDOW_SERVICE);  
  315.         windowManager.addView(overlay, lp);  
  316.     }  
  317.   
  318.     private class LetterListViewListener implements  
  319.             OnTouchingLetterChangedListener {  
  320.   
  321.         @Override  
  322.         public void onTouchingLetterChanged(final String s) {  
  323.             if (alphaIndexer.get(s) != null) {  
  324.                 int position = alphaIndexer.get(s);  
  325.                 personList.setSelection(position);  
  326.                 overlay.setText(sections[position]);  
  327.                 overlay.setVisibility(View.VISIBLE);  
  328.                 handler.removeCallbacks(overlayThread);  
  329.                 // 延迟一秒后执行,让overlay为不可见  
  330.                 handler.postDelayed(overlayThread, 1500);  
  331.             }  
  332.         }  
  333.   
  334.     }  
  335.   
  336.     // 设置overlay不可见  
  337.     private class OverlayThread implements Runnable {  
  338.         @Override  
  339.         public void run() {  
  340.             overlay.setVisibility(View.GONE);  
  341.         }  
  342.   
  343.     }  
  344.   
  345.     // 获得汉语拼音首字母  
  346.     private String getAlpha(String str) {  
  347.   
  348.         if (str.equals("-")) {  
  349.             return "&";  
  350.         }  
  351.         if (str == null) {  
  352.             return "#";  
  353.         }  
  354.         if (str.trim().length() == 0) {  
  355.             return "#";  
  356.         }  
  357.         char c = str.trim().substring(01).charAt(0);  
  358.         // 正则表达式,判断首字母是否是英文字母  
  359.         Pattern pattern = Pattern.compile("^[A-Za-z]+$");  
  360.         if (pattern.matcher(c + "").matches()) {  
  361.             return (c + "").toUpperCase();  
  362.         } else {  
  363.             return "#";  
  364.         }  
  365.     }  
  366.   
  367.     /** 
  368.      *  
  369.      * 初始化GPS 
  370.      * @author yayun 
  371.      * @since 2015年9月18日 
  372.      */  
  373.     private void initGps() {  
  374.         try{  
  375.             MyLocationListenner myListener = new MyLocationListenner();  
  376.             locationClient = new LocationClient(ActivitySelectCity.this);   
  377.             locationClient.registerLocationListener(myListener);  
  378.             LocationClientOption option = new LocationClientOption();  
  379.             option.setOpenGps(true);  
  380.             option.setAddrType("all");  
  381.             option.setCoorType("bd09ll");  
  382.             option.setScanSpan(5000);  
  383.             option.disableCache(true);  
  384.             option.setPoiNumber(5);   
  385.             option.setPoiDistance(1000);   
  386.             option.setPoiExtraInfo(true);   
  387.             option.setPriority(LocationClientOption.GpsFirst);  
  388.             locationClient.setLocOption(option);  
  389.             locationClient.start();  
  390.         }catch (Exception e) {  
  391.             e.printStackTrace();  
  392.         }  
  393.     }  
  394.   
  395.     /** 
  396.      * 销毁 
  397.      */  
  398.     @Override  
  399.     public void onDestroy() {  
  400.         super.onDestroy();  
  401.         locationClient.stop();  
  402.     }  
  403.   
  404.     /** 
  405.      * 通过位置SDK获取现在所在城市 
  406.      * @author yayun 
  407.      * @since 2015年9月18日 
  408.      * 
  409.      */  
  410.   
  411.     private class MyLocationListenner implements BDLocationListener {  
  412.         @Override  
  413.         public void onReceiveLocation(BDLocation location) {  
  414.   
  415.             if (location == null)  
  416.                 return;  
  417.             StringBuffer sb = new StringBuffer(256);  
  418.             if (location.getLocType() == BDLocation.TypeGpsLocation) {  
  419.                 sb.append(location.getCity());  
  420.             } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {  
  421.                 sb.append(location.getCity());  
  422.             }  
  423.             if (sb.toString() != null && sb.toString().length() > 0) {  
  424.                 lngCityName=sb.toString();  
  425.                 lng_city.setText(lngCityName);  
  426.             }  
  427.   
  428.         }  
  429.   
  430.         public void onReceivePoi(BDLocation poiLocation) {  
  431.   
  432.         }  
  433.     }  
  434.       
  435.     Handler handler2 = new Handler(){  
  436.         public void handleMessage(android.os.Message msg) {  
  437.             switch (msg.what) {  
  438.             case SHOWDIALOG:  
  439.                 progress = AppUtil.showProgress(ActivitySelectCity.this"正在加载数据,请稍候...");  
  440.                 break;  
  441.             case DISMISSDIALOG:  
  442.                 if (progress != null)  
  443.                 {  
  444.                     progress.dismiss();  
  445.                 }  
  446.                 adapter = new ListAdapter(ActivitySelectCity.this);  
  447.                 personList.setAdapter(adapter);  
  448. //              personList.setAdapter(adapter);  
  449.                   
  450.                 sh.addTextChangedListener(new TextWatcher() {  
  451.                     @Override  
  452.                     public void onTextChanged(CharSequence s, int start, int before, int count) {  
  453.                     }  
  454.                     @Override  
  455.                     public void beforeTextChanged(CharSequence s, int start, int count,  
  456.                             int after) {  
  457.                     }  
  458.                     @Override  
  459.                     public void afterTextChanged(Editable s) {  
  460.                         //搜索符合用户输入的城市名  
  461.                         if(s.length()>0){  
  462.                             ArrayList<City> changecity = new ArrayList<City>();  
  463.                             for(int i=0;i<city_lists.size();i++){  
  464.                                 if(city_lists.get(i).name.indexOf(sh.getText().toString())!=-1){  
  465.                                     changecity.add(city_lists.get(i));  
  466.                                 }  
  467.                             }  
  468.                             ShowCity_lists = changecity;  
  469.                         }else{  
  470.                             ShowCity_lists = allCity_lists;  
  471.                         }  
  472.                         adapter.notifyDataSetChanged();  
  473.                     }  
  474.                 });  
  475.                 break;  
  476.             default:  
  477.                 break;  
  478.             }  
  479.         };  
  480.     };  
  481. }  

3.自定义控件(快速索引):

[java]  view plain copy
  1. package com.winxiang.locationselect;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Typeface;  
  8. import android.text.style.TypefaceSpan;  
  9. import android.util.AttributeSet;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12.   
  13. public class MyLetterListView extends View {  
  14.   
  15.     OnTouchingLetterChangedListener onTouchingLetterChangedListener;  
  16.     String[] b = { "#""A""B""C""D""E""F""G""H""I""J""K",  
  17.             "L""M""N""O""P""Q""R""S""T""U""V""W""X",  
  18.             "Y""Z" };  
  19.     int choose = -1;  
  20.     Paint paint = new Paint();  
  21.     boolean showBkg = false;  
  22.   
  23.     public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {  
  24.         super(context, attrs, defStyle);  
  25.     }  
  26.   
  27.     public MyLetterListView(Context context, AttributeSet attrs) {  
  28.         super(context, attrs);  
  29.     }  
  30.   
  31.     public MyLetterListView(Context context) {  
  32.         super(context);  
  33.     }  
  34.   
  35.     @Override  
  36.     protected void onDraw(Canvas canvas) {  
  37.         super.onDraw(canvas);  
  38.         if (showBkg) {  
  39.             canvas.drawColor(Color.parseColor("#10000000"));  
  40.         }  
  41.         int height = getHeight();  
  42.         int width = getWidth();  
  43.         int singleHeight = height / b.length;  
  44.         for (int i = 0; i < b.length; i++) {  
  45.             paint.setColor(Color.parseColor("#515151"));  
  46.             paint.setTypeface(Typeface.DEFAULT_BOLD);  
  47.             paint.setAntiAlias(true);  
  48.             if (i == choose) {  
  49.                 paint.setColor(Color.parseColor("#3399ff"));  
  50.                 paint.setFakeBoldText(true);  
  51.             }  
  52.             float xPos = width / 2 - paint.measureText(b[i]) / 2;  
  53.             float yPos = singleHeight * i + singleHeight;  
  54.             canvas.drawText(b[i], xPos, yPos, paint);  
  55.             paint.reset();  
  56.         }  
  57.   
  58.     }  
  59.   
  60.     @Override  
  61.     public boolean dispatchTouchEvent(MotionEvent event) {  
  62.         final int action = event.getAction();  
  63.         final float y = event.getY();  
  64.         final int oldChoose = choose;  
  65.         final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;  
  66.         final int c = (int) (y / getHeight() * b.length);  
  67.         switch (action) {  
  68.         case MotionEvent.ACTION_DOWN:  
  69.             showBkg = true;  
  70.             if (oldChoose != c && listener != null) {  
  71.                 if (c >= 0 && c <= b.length) {  
  72.                     listener.onTouchingLetterChanged(b[c]);  
  73.                     choose = c;  
  74.                     invalidate();  
  75.                 }  
  76.             }  
  77.   
  78.             break;  
  79.         case MotionEvent.ACTION_MOVE:  
  80.             if (oldChoose != c && listener != null) {  
  81.                 if (c >= 0 && c <= b.length) {  
  82.                     listener.onTouchingLetterChanged(b[c]);  
  83.                     choose = c;  
  84.                     invalidate();  
  85.                 }  
  86.             }  
  87.             break;  
  88.         case MotionEvent.ACTION_UP:  
  89.             showBkg = false;  
  90.             choose = -1;  
  91.             invalidate();  
  92.             break;  
  93.         }  
  94.         return true;  
  95.     }  
  96.   
  97.     @Override  
  98.     public boolean onTouchEvent(MotionEvent event) {  
  99.         return super.onTouchEvent(event);  
  100.     }  
  101.   
  102.     public void setOnTouchingLetterChangedListener(  
  103.             OnTouchingLetterChangedListener onTouchingLetterChangedListener) {  
  104.         this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;  
  105.     }  
  106.   
  107.     public interface OnTouchingLetterChangedListener {  
  108.         public void onTouchingLetterChanged(String s);  
  109.     }  
  110.   
  111. }  


运行一下效果:



介于篇幅问题,其他代码大家可以下载源码查看:

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值