根據輸入框的輸入內容的不同,來檢索本地通訊錄,是按照姓名,還是手機號碼!

login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout 
  	  android:layout_gravity="center"
	  android:layout_width="fill_parent"
	  android:layout_height="wrap_content"
	  android:orientation="vertical"
	  android:padding="15.0dip">
	  <TextView 
		  android:text="用戶名"   
		  android:textSize="20sp"
		  android:layout_width="fill_parent"
		  android:layout_height="wrap_content" >
	  </TextView><!-- 311613 lw8p999aar    34171457 js7m78cwdd -->
	  <EditText 
		  android:layout_width="fill_parent"
		  android:text="311613"
		  android:layout_height="40dp"
		  android:id="@+id/userName">
	  </EditText>
      <TextView 
	      android:text="密碼"
	      android:textSize="20sp"
	      android:layout_width="fill_parent"
		  android:layout_height="wrap_content">
      </TextView>
      <EditText   
		  android:layout_width="fill_parent"
		  android:text="lw8p999aar"
		  android:layout_height="40dp" 
		  android:id="@+id/userPassWord" 
		  android:password="true">
	  </EditText> 
	  <LinearLayout 
	  	  android:gravity="center_horizontal"
		  android:layout_width="fill_parent"
		  android:layout_height="wrap_content"
		  android:orientation="horizontal">
		  <Button 
			  android:id="@+id/logBtn"
			  android:layout_width="60.0dp"
			  android:layout_height="50.0dp"
			  android:layout_gravity="center"
			  android:text="登陸"
			  android:focusable="true"
			  android:layout_margin="10.0dp">
	  	  </Button>
	  	  <Button 
			  android:id="@+id/cancleBtn"
			  android:layout_width="60.0dp"
			  android:layout_height="50.0dp"
			  android:text="取消"
			  android:layout_margin="10.0dp">
	  	  </Button>
	  </LinearLayout>
  </LinearLayout>
</LinearLayout>


首先,先要讓輸入框獲得焦點,然後間隔1秒鐘後,彈出軟鍵盤

其次,根據輸入框的輸入內容的不同。來響應的檢索

 

package com.shen;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */

	private InputMethodManager imm = null;
	private EditText login_Edittext;
	private static String debug = "MainActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		imm = (InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		login_Edittext = (EditText) findViewById(R.id.userName);
		login_Edittext.requestFocus();
		Timer timer = new Timer(); // 設置定時器
		timer.schedule(new TimerTask() {
			@Override
			public void run() { // 彈出軟鍵盤的代碼
				imm.showSoftInput(login_Edittext,
						InputMethodManager.HIDE_NOT_ALWAYS);
				imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
			}
		}, 300); // 設置300毫秒的時長

		login_Edittext.addTextChangedListener(changeWather);
		// List<InputMethodInfo> infoList = imm.getInputMethodList();
		// infoList.size();
		// List<InputMethodInfo> enableList = imm.getEnabledInputMethodList();
		// enableList.size();
		// Log.e(debug, "infoList.size()==" + infoList.size());
		// Log.e(debug, "enableList.size()==" + enableList.size());
		// InputMethodInfo info=enableList.get(0);
		// info.

	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		Log.e(debug, "event.getAction()==" + event.getAction());
		return super.dispatchKeyEvent(event);

	}

	private TextWatcher changeWather = new TextWatcher() {
		public void afterTextChanged(Editable s) {
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			String textValue = login_Edittext.getText().toString();
			if (textValue.length() > 0) {
//				if (isChineseEnglish(textValue.charAt(0))) {
//					Log.e(debug, "按照中文搜索******");
//				} else {
//					Log.e(debug, "按照英文來搜索==========");
//				}
//				isChinese(textValue);
				hasChinese(textValue);
//				Log.e(debug, isChinese(textValue));

			}
		};
	};
	
	
	/**
	 * 判斷一個字符是中文還是英文
	 * @param c
	 * @return
	 */
	public static boolean isChineseEnglish(char c) {
		if (c >= 0 && c <= 9) {
			// 是數字
			return false;//"是數字字符";
		} else if ((c >= 'a' && c <= 'z')) { // 是小寫字母
			return false;//"是小寫字母";
		} else if ((c >= 'A' && c <= 'z')) { // 是大寫字母
			return false;//"是大寫字母";
		} else if (Character.isLetter(c)) { // 是漢字
			return true;//"是漢字字符";
		} else { // 是特殊符號
			return false;//"是特殊符號";
		}
	}
	
	/**
	 * 判斷一個字符串中是否含有中文
	 * @param chinese
	 * @return
	 */
	static public String isChinese(String chinese) {
		String ucode = " ";
		String strChinese = " ";
		String strASC = " ";
		try {
			int clen;
			clen = chinese.length(); // 取字符串長度
			String utemp = " ";
			char[] strBuffer = chinese.toCharArray(); // 將字符串轉化爲字符數組
			int l; // 每個字符轉換後的二進制字符串的長度
			int s;

			for (int i = 0; i < clen; i++) {
				s = (int) strBuffer[i]; // 取一個字符
				utemp = Integer.toHexString(s).toUpperCase();
				l = utemp.length();
				if (l <= 2) { // 如果是ASC字符
					utemp = "00 " + utemp;
					// 保存ASC字符到strASC
					strASC += chinese.substring(i, i + 1);
				} else {
					// 保存中文字符到strChinese
					strChinese += chinese.substring(i, i + 1);
				}
				ucode = ucode + utemp;
			}
			System.err.println(strASC);
			System.err.println(strChinese);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strChinese;

	} 
	
	public void hasChinese(String str){
		System.out.println(str.length()==str.getBytes().length? "English ": "Chinese ");
	}



}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章