根据输入框的输入内容的不同,来检索本地通讯录,是按照姓名,还是手机号码!

来源:互联网 发布:c语言表达式基本类型 编辑:程序博客网 时间:2024/05/03 03:43
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";@Overridepublic 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() {@Overridepublic 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.}@Overridepublic 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字符到strASCstrASC += chinese.substring(i, i + 1);} else {// 保存中文字符到strChinesestrChinese += 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 ");}}


 

原创粉丝点击