正则表达式完成号码验证,
^以某开头,[] 字符集(匹配中括号里面的任意字符),/d是任意一个数字,{n}表示出现了多少次,$结尾
手机号的正则 ^1[34568]/d{9}$,以1开头,第二个数字是34568,剩下九个都是数字,数字结尾
调用String对象的match()方法,参数:正则(里面的斜杠/需要转义)
if (phone.matches("^1[34568]//d{9}$")) { SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); Cursor cursor = db .rawQuery( "select location from data2 where id=(select outkey from data1 where id=?)", new String[] { phone.substring(0, 7) }); while (cursor.moveToNext()) { String address = cursor.getString(0); return address; } cursor.close(); }
文本输入直接查询
调用EditText对象的addTextChangedListener()方法,添加监听器,参数:TextWatcher对象
匿名内部类实现TextWatcher接口,重写onTextChanged方法,传递进来CharSequence对象
查询数据库,并展示出结果
//自动完成 et_phone.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) { phone = et_phone.getText().toString().trim(); String result = NumberQueryAddressUtil.queryAddress(phone); tv_address.setText(result); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } });
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5415.html