码迷,mamicode.com
首页 > 移动开发 > 详细

《Android开发卷——手机联系人T9搜索》

时间:2014-06-11 06:15:03      阅读:443      评论:0      收藏:0      [点我收藏+]

标签:获取手机联系人   contract   t9搜索   android   拨号算法   

bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣 

自己研究了好几天联系人的T9搜索算法, 先分享出来给大家看看. 欢迎指教.如果有大神有更好的T9搜索算法, 那更好啊,大家一起研究研究,谢谢.

第一部分是比较简单的获取手机联系人.

获取联系人前提要有权限.

<uses-permission android:name="android.permission.READ_CONTACTS" />

因为手机的联系人都存储在数据库里面,所以我们只要把数据库里的信息查询出来即可.

private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.SORT_KEY_ALTERNATIVE};

ContentResolver resolver = getBaseContext().getContentResolver();
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
PHONES_PROJECTION, null, null, null);

这里我只是简单的说一下而已,后面的遍历phoneCursor 就能把联系人查询出来.(我项目里是开一条线程获取联系人的,以防太多联系人导致页面空白或者卡顿)


第二部分是T9搜索部分

思路是: a只要联系人的号码有包含我输入的数字就add到list进去,

b联系人的姓名拼音有包含我输入的拼音就add到list进去,

c联系人的姓名缩写拼音有包含我输入的拼音就add到list进去

举个例子--联系人姓名:测试,号码:1234567890

情况一:我输入1234567890就能把测试显示出来

情况二:我输入23744(ceshi),因为这个"测试"的拼音,所以也能把测试显示出来

情况三:我输入(27)cs,这是"测试"的拼音缩写,所以也能把测试显示出来

当然我输入23(ce)或者744(shi)什么的,都可以把测试显示出来.

以上是我的思路,下面附上一段代码

public class MainT9 extends Activity{

	private int[] textViewId = new int[]{R.id.main_num_1,R.id.main_num_2,R.id.main_num_3,R.id.main_num_4,
			R.id.main_num_5,R.id.main_num_6,R.id.main_num_7,R.id.main_num_8,R.id.main_num_9,R.id.main_num_left,
			R.id.main_num_0,R.id.main_num_right,R.id.main_num_delete};
	private TextView[] textView = new TextView[textViewId.length];
	private EditText inputEditText;
	
	private ListView mListView;
	
	private List<ContractBean> mList;
	private List<ContractBean> list;
	private MyAdapter myAdapter;
	
	private static final String[] PHONES_PROJECTION = new String[] {
		Phone.DISPLAY_NAME, Phone.NUMBER, Photo.SORT_KEY_ALTERNATIVE};
	
	long waitTime = 300;  
	long touchTime = 0;
	
	/**
	 * 判断是否人为输入
	 */
	private boolean isWrite = false;
	private boolean isAfresh = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main_activity);
		
		
		mList = new ArrayList<ContractBean>();
		list = new ArrayList<ContractBean>();
		
		/**
		 * 刚开始启动程序时,开条线程去获取联系人
		 */
		new Thread(getContract).start();
		
		for(int i = 0;i<textViewId.length;i++){
			textView[i] = (TextView) this.findViewById(textViewId[i]);
			textView[i].setOnClickListener(click);
		}
		
		inputEditText = (EditText) this.findViewById(R.id.main_num_edit);
//		hideSystemKeyBoard(inputEditText);
		
		mListView = (ListView) this.findViewById(R.id.main_contract_listview);
		myAdapter = new MyAdapter(this, mList);
		mListView.setAdapter(myAdapter);
		
		inputEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				if (isWrite) {
					isWrite = false;
					return;
				}
				isWrite = true;
				String inputStr = "";
				String newStr = s.toString();
				newStr = newStr.replace(" ", "");
				int index = 0;
				if (true) {
					if ((index + 3) < newStr.length()) {
						inputStr += (newStr.substring(index, index + 3) + " ");
						index += 3;
					}
				}
				while ((index + 4) < newStr.length()) {
					inputStr += (newStr.substring(index, index + 4) + " ");
					index += 4;
				}
				inputStr += (newStr.substring(index, newStr.length()));
				inputEditText.setText(inputStr);
				inputEditText.setSelection(inputStr.length());
				
				if(count == 0){
					isAfresh = true;
				}else{
					isAfresh = false; 
				}
				
				long currentTime = System.currentTimeMillis();
				if ((currentTime - touchTime) >= waitTime) {
					touchTime = currentTime;
					searchConstract(isAfresh);
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
			}
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
			}
		});
	}
	
	private void searchConstract(boolean isAfresh){
		if(isAfresh){
			if(mList!=null){
				myAdapter.updateListView(mList);
			}else{
				new Thread(getContract).start();
			}
			
		}else{
			String  str1= inputEditText.getText().toString();
			str1 = str1.replace(" ", "");
			
			SearchContract search = new SearchContract(str1);
			search.start();
		}
	}
	
	/**
	 * T9搜索线程
	 * @author Chillax_KUN
	 */
	class SearchContract extends Thread{
		String body;
		SearchContract(String body){
			this.body = body;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			Iterator<ContractBean> iterator = mList.iterator();
			list = new ArrayList<ContractBean>();
			list.clear();
			
			while(iterator.hasNext()){
				ContractBean sortModel = iterator.next();
				String search = sortModel.getSearch();
				String phone = sortModel.getPhone();
				phone = phone.replace(" ", "");
				String zimu = sortModel.getZimu();
				
				if(phone.contains(body) || search.contains(body)||zimu.contains(body)){
					list.add(sortModel);
				}
			}
			h.sendEmptyMessage(1);
		}
		
	}
	
	/**
	 * 开启线程,获取联系人
	 */
	Runnable getContract = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			mList = getPhoneContacts();
			h.sendEmptyMessage(0);
		}
	};
	
	private List<ContractBean> getPhoneContacts() {
		ContentResolver resolver = getBaseContext().getContentResolver();
		Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
				PHONES_PROJECTION, null, null, null);
		
		List<ContractBean> mList = new ArrayList<ContractBean>();
		
		if (phoneCursor != null) {
			phoneCursor.moveToFirst();
			while (!phoneCursor.isAfterLast()) {
				ContractBean model = new ContractBean();
				
				String name = phoneCursor.getString(0);
				String phone = phoneCursor.getString(1);
				String search = phoneCursor.getString(2);
				
				model.setName(name);
				model.setPhone(phone);
				model.setSearch(getNum(search,false));
				
				String zimu = "";
				String str[] = search.split(" ");
				if(str.length>0){
					for(int i = 0;i<str.length;i++){
						if(str[i].length()==1){
							zimu = zimu + str[i];
						}
					}
				}

				model.setZimu(getNum(zimu,true));
				
				mList.add(model);
				phoneCursor.moveToNext();
			}
			if(phoneCursor!=null){
				phoneCursor.close();
			}
			return mList;
		}
		return mList;
	}
	
	/**
	 * 将联系人的姓名拼音全部转化为数字
	 * @param 联系人姓名拼音
	 * @return 姓名拼音对应数字
	 */
	public String getNum(String search, boolean status){
		String str = "";
		for(int i = 0;i<search.length();i++){
			String c = search.charAt(i)+"";
			if(c.equals("1")){
				str = str + "1";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("A")||c.equals("B")||c.equals("C")||c.equals("2")
					||c.equals("a")||c.equals("b")||c.equals("c")){
				str = str + "2";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("D")||c.equals("E")||c.equals("F")||c.equals("3")
					||c.equals("d")||c.equals("e")||c.equals("f")){
				str = str + "3";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("G")||c.equals("H")||c.equals("I")||c.equals("4")
					||c.equals("g")||c.equals("h")||c.equals("i")){
				str = str + "4";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("J")||c.equals("K")||c.equals("L")||c.equals("5")
					||c.equals("j")||c.equals("k")||c.equals("l")){
				str = str + "5";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("M")||c.equals("N")||c.equals("O")||c.equals("6")
					||c.equals("m")||c.equals("n")||c.equals("o")){
				str = str + "6";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("P")||c.equals("Q")||c.equals("R")||c.equals("S")||c.equals("7")
					||c.equals("p")||c.equals("q")||c.equals("r")||c.equals("s")){
				str = str + "7";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("T")||c.equals("U")||c.equals("V")||c.equals("8")
					||c.equals("t")||c.equals("u")||c.equals("v")){
				str = str + "8";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("W")||c.equals("X")||c.equals("Y")||c.equals("Z")||c.equals("9")
					||c.equals("w")||c.equals("x")||c.equals("y")||c.equals("z")){
				str = str + "9";
				if(status){
					i = i + 1;
				}
				continue;
			}else if(c.equals("0")){
				str = str + "0";
				if(status){
					i = i + 1;
				}
				continue;
			}
		}
		return str;
	};
	
	Handler h = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(msg.what==0){
				if(mList!=null && myAdapter!=null){	
					myAdapter.updateListView(mList);
					myAdapter.notifyDataSetChanged();
				}
			} else if(msg.what==1){
				myAdapter.updateListView(list);
			}
			
		}
	};
	
	OnClickListener click = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.main_num_1:
				inputEditText.setText(inputEditText.getText()+"1");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_2:
				inputEditText.setText(inputEditText.getText()+"2");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_3:
				inputEditText.setText(inputEditText.getText()+"3");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_4:
				inputEditText.setText(inputEditText.getText()+"4");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_5:
				inputEditText.setText(inputEditText.getText()+"5");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_6:
				inputEditText.setText(inputEditText.getText()+"6");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_7:
				inputEditText.setText(inputEditText.getText()+"7");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_8:
				inputEditText.setText(inputEditText.getText()+"8");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_9:
				inputEditText.setText(inputEditText.getText()+"9");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_0:
				inputEditText.setText(inputEditText.getText()+"0");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_left:
				inputEditText.setText(inputEditText.getText()+"*");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_right:
				inputEditText.setText(inputEditText.getText()+"#");
				inputEditText.setSelection(inputEditText.getText().toString().length());
				break;
			case R.id.main_num_delete:
				String string = inputEditText.getText().toString();
				if(!string.equals("")){
					inputEditText.setText(string.subSequence(0, string.length()-1));
					inputEditText.setSelection(inputEditText.getText().toString().length());
				}
				break;
			default:
				break;
			}
		}
	};
	
	/**
	 * 通过反射调用setShowSoftInputOnFocus(false) 来隐藏键盘。 用 InputType.TYPE_NULL方法,无法显示光标。
	 */
	private void hideSystemKeyBoard(View view) {
		this.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		try {
			Class<EditText> cls = EditText.class;
			Method setSoftInputShownOnFocus;
			// 此方法为隐藏的需用java反射调用
			setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus",
					boolean.class);
			setSoftInputShownOnFocus.setAccessible(true);
			setSoftInputShownOnFocus.invoke((EditText) view, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


源码地址:http://download.csdn.net/detail/chillax_li/7467553


尊重原创, 转载请注明出处:http://blog.csdn.net/chillax_li/article/details/29380615


《Android开发卷——手机联系人T9搜索》,布布扣,bubuko.com

《Android开发卷——手机联系人T9搜索》

标签:获取手机联系人   contract   t9搜索   android   拨号算法   

原文地址:http://blog.csdn.net/chillax_li/article/details/29380615

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!