号码归属地查询&设置中心(自定义组合控件)&震动器 &监听输入EditText(输入监听器TextWatcher)

OnClickListener OnItemClickListener

① 创建Activity 布局  写事件 输入监听器TextWatcher  1.继承 2.重写3.配置4.启动

// ① 创建Activity 布局 写事件 输入监听器 1.继承 2.重写3.配置4.启动
        TextWatcher watcher = new TextWatcher() {

            // 监听输入内容
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // Sequence序列
                // CharSequence--》String
                String inputString = s.toString().trim();

                String addressString = dao.findAddress(inputString);
                address.setText(addressString);
                Log.i("wzx", inputString);
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        // 添加输入监听器TextChanged字符串变化
        input.addTextChangedListener(watcher);
        // ② 复制数据到sd 目录 /data/data/包名/databases
        // ③ 编写sql 转成代码Dao
        // ④ 空提示 动画特效
        // ⑤ 震动器
    }

② 编写sql  转成代码Dao(sqlite 相关api)

public class AddressDao {

    private Context context;

    public AddressDao(Context context) {
        super();
        this.context = context;
    }

    private String path = "/data/data/com.itheima.mobilesafe/databases/";

    public String findAddress(String number) {
        String address = "未知";
        // copyDb();

        // 手机号 13168313887

        // 打开一个已存的数据库
        // SQLiteDatabase db=SQLiteDatabase.openDatabase(路径 , 游标工厂 null, 打开方式);
        SQLiteDatabase db = SQLiteDatabase.openDatabase(path + "address.db", null, SQLiteDatabase.OPEN_READONLY);
        //
        if (StringUtils.isPhone(number)) {
            // 合并成一个 sql
            String id = number.substring(0, 7);
            String sql1 = "select location from data2 where id=(select outkey from data1 where id=?)";
            Cursor cursor = db.rawQuery(sql1, new String[] { id });
            if (cursor.moveToNext()) {
                address = cursor.getString(cursor.getColumnIndex("location"));
            }
            cursor.close();
        } else {

            if (number.startsWith("0") && number.length() >= 3) {
                // 一线城市 010 020 subString(0,3)
                String sql2 = "select location from data2 where area=?;";
                String area = number.substring(1, 3);
                Cursor cursor = db.rawQuery(sql2, new String[] { area });
                if (cursor.moveToNext()) {
                    address = cursor.getString(cursor.getColumnIndex("location"));
                    Log.i("wzx", "一线城市 "+address);
                }
                cursor.close();
                // 二线城市 0591
                Log.i("wzx", "一线城市 "+address);
                if ("未知".equals(address)&& number.length() >= 4 || null == address) {
                    // select location from data2 where area='591';
                    area = number.substring(1, 4);
                    Cursor cursor2 = db.rawQuery(sql2, new String[] { area });
                    if (cursor2.moveToNext()) {
                        address = cursor2.getString(cursor2.getColumnIndex("location"));
                        Log.i("wzx", "二线城市 "+address);
                    }
                    cursor2.close();
                }

                // 去 移动联通 电信
                if (!"".equals(address) || null == address) {
                    address = address.replace("移动", "固话");
                    address = address.replace("联通", "固话");
                    address = address.replace("电信", "固话");
                }
            }
            // 固话
            // 固话
        }

        // 关闭
        db.close();
        // 数据库
        // 判断 手机号 还是固话
        return address;

    }

    /**
     * 导入数据库
     */
    public void copyDb() {
    
        try {
            InputStream input = context.getAssets().open("address.db");
            FileUtils.copyFile(input, path, "address.db");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }    
}

③ 复制数据到sd 目录 /data/data/包名/databases

④ 正则表达式 :较验字符串格式的语法  数字+9  ^1[345678][0-9{9}]$

5, 空提示  动画特效  

Animation

   |--TranslateAnimation移动动画

“变化率”interpolator

加速

 

减速

 

加速 后减速

 

匀速

 

AnimationUtils

anim 的动画文件生成Animation对象

view.startAnimation(anim)添加动画

 

@OnClick(R.id.btn_query)

public void query(View view) {

String inputvalue = input.getText().toString().trim();

if ("".equals(inputvalue)) {

Animation anim = AnimationUtils.loadAnimation(this, R.anim.shake);

input.startAnimation(anim);

 

Toast.makeText(getBaseContext(), "号码不能为空!"0).show();

return;

}

}

 

② 震动器  

VIBRATE

设置节奏  300 100 300 100 300 100.。。

短信  flag  -1  一次

闹钟       1  无限

手机上的一个硬件。

gps-->定位芯片  权限  系统级服务

Camera -->权限 

开发步骤

① 权限

② 系统服务

③ 设置节奏

<!--     使用震动器 -->

<uses-permission android:name="android.permission.VIBRATE"/>

//硬件

Vibrator  vibrator=(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

//设置节奏 + 开始

vibrator.vibrate(new long[]{300,100,300,100,300,100}, -1);


需求:1.设置中心是一个通用的功能。2.一个开关的集合,控制功能上的参数。

 

 

自定义组合控件

1.开发者自己开发标签 不属于android.jar

2.由基本控件组成

3.通过属性控件 基本控件显示

 

 

 

设置中心(自定义组合控件)

开发步骤

① 开发标签  显示需要的内容  1.继承布局(五大布局)2.添加视图3.配置

② 开发属性 必须合法1.学习 attrs.xml申明 2.引入xml进行申明 

adt  属性申明 但不引入到xml中   凤姐  绿卡  携带

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"

③ 标签识别配置的属性值 1.读取属性值  2.初始化控件 3.设置值 

 <!-- attr申明自定义属性 -->

    <!-- reference @string/.. -->

    <!-- format变量类型 -->

    <declare-styleable name="SetItemView">

        <attr name="title" format="string|reference" />

        <attr name="desc_on" format="string|reference" />

        <attr name="desc_off" format="string|reference" />

        <attr name="check" format="boolean" />

    </declare-styleable>

 

 

  <!-- android.jar 不写包名 -->

    <!-- 自定义的控件不写包名 SetItemView 系统默认去android.jar找 -->

 

    <com.itheima.mobilesafe.view.SetItemView

        android:id="@+id/update_item"

        itheima:title="提示更新"

        itheima:desc_on="已经打开提示更新"

        itheima:desc_off="已经关闭提示更新"

        itheima:check="false"

        android:layout_width="match_parent"

        android:layout_height="60dp" />

 

<span style="font-size:14px;">public class SetItemView extends LinearLayout {
	TextView desc;
	CheckBox checkbox;
	TextView title;
	String desc_onValue;
	String desc_offValue;
	// 提供给xml配置使用
	public SetItemView(Context context, AttributeSet attrs) {
		super(context, attrs);

		// inflate打气 将xml--》转换成View对象
		View view = View.inflate(context, R.layout.view_item_set_checkbox, null);
		super.addView(view);
		// AttributeSet集合:标签配置的所有属性=android自带+自定义
		// int count=attrs.getAttributeCount();
		// Log.i("wzx", "-getAttributeCount--"+count);
		//
		// for(int i=0;i<count;i++)
		// {
		// Log.i("wzx", attrs.getAttributeValue(i));
		//
		// }
		String ns = "http://schemas.android.com/apk/res/com.itheima.mobilesafe";
		String titleVlaue = attrs.getAttributeValue(ns, "title");
		desc_onValue = attrs.getAttributeValue(ns, "desc_on");
		desc_offValue = attrs.getAttributeValue(ns, "desc_off");
		boolean checkValue = attrs.getAttributeBooleanValue(ns, "check", false);

		title = (TextView) view.findViewById(R.id.title);
		desc = (TextView) view.findViewById(R.id.dec);
		checkbox = (CheckBox) view.findViewById(R.id.checkbox);
		checkbox.setChecked(checkValue);
		title.setText(titleVlaue == null ? "" : titleVlaue);
		desc.setText(checkValue ? desc_onValue : desc_offValue);

		// ① 开发标签 显示需要的内容 1.继承布局(五大布局)2.添加视图3.配置
		// ② 开发属性 必须合法
		// ③ 标签识别配置的属性值
	}


	/**
	 * 设置选中状态 
	 * @param checkValue
	 */
	public void setCheck(Boolean checkValue) {
		checkbox.setChecked(checkValue);
		desc.setText(checkValue ? desc_onValue : desc_offValue);
	}
	public boolean isCheck()
	{
		return checkbox.isChecked();
	}

}
	// 获取到网络版本号
			boolean update_itemValue = SharedPreferencesUtils.getBoolean(this, "update_item", false);
			if (update_itemValue) {
				// 开始更新流程
				Toast.makeText(getBaseContext(), "开始自动更新", 0).show();
				checkVersion();
			} else {
				// 自动更新关闭  //2000
				new Thread(){
					public void run() {
						try {
							Thread.sleep(2000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						loadMainUI();
					};
				}.start();
			}</span>

// ① 开发标签 显示需要的内容 1.继承布局(五大布局)2.添加视图3.配置

// ② 开发属性 必须合法

// ③ 标签识别配置的属性值

}

 

<span style="font-size:14px;">/**
	 * 设置选中状态 
	 * @param checkValue
	 */
	public void setCheck(Boolean checkValue) {
		checkbox.setChecked(checkValue);
		desc.setText(checkValue ? desc_onValue : desc_offValue);
	}
	public boolean isCheck()
	{
		return checkbox.isChecked();
	}

}</span>

注意事项

使用组合控件 

1.布局重复出现多的情况  使用组合控件 代码比较少--》快

2.// inflate打气 将xml--》转换成View对象

// View view = View.inflate(context, R.layout.view_item_set_checkbox,

// null);

// super.addView(view);

 

// 第三个参数 this 当前标签 或者布局 传inflate 1.==null addView 2.!=null addView

View view = View.inflate(context, R.layout.view_item_set_checkboxthis);

 

① 创建Activity 布局ui

② 写事件

③ 保存变量

④ 找到对应的功能 写条件























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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值