zoukankan      html  css  js  c++  java
  • [android] 手机卫士号码归属地查询完成

    正则表达式完成号码验证,

    ^以某开头,[] 字符集(匹配中括号里面的任意字符),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
                    
                }
            });
  • 相关阅读:
    Python(调用函数、定义函数)
    Python(可变/不可变类型,list,tuple,dict,set)
    Python(变量、数据类型)
    java内存泄露
    java垃圾回收
    mac下安装mysql教程
    Http、Https请求工具类
    ThreadLocal内部机制及使用方法
    java单例模式详解
    (转)Java集合框架:HashMap
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5425608.html
Copyright © 2011-2022 走看看