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

编程入门 行业动态 更新时间:2024-10-12 22:33:32

号码归属地查询设置中心(自定义组合控件)震动器  监听输入EditText(输入<a href=https://www.elefans.com/category/jswz/34/1760609.html style=监听器TextWatcher)"/>

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

OnClickListener OnItemClickListener

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

// ① 创建Activity 布局 写事件 输入监听器 1.继承 2.重写3.配置4.启动TextWatcher watcher = new TextWatcher() {// 监听输入内容@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {// Sequence序列// CharSequence--》StringString inputString = s.toString().trim();String addressString = dao.findAddress(inputString);address.setText(addressString);Log.i("wzx", inputString);}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic 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)) {// 合并成一个 sqlString 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();// 二线城市 0591Log.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 blocke.printStackTrace();}}    
}

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

④ 正则表达式 :较验字符串格式的语法  1 数字+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=""

xmlns:itheima=".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 = ".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 {// 自动更新关闭  //2000new Thread(){public void run() {try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.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_checkbox, this);

 

① 创建Activity 布局ui

② 写事件

③ 保存变量

④ 找到对应的功能 写条件























更多推荐

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

本文发布于:2024-02-26 13:49:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1702728.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:监听器   组合   自定义   控件   号码归属地查询

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!