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

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 ");
	}



}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值