您的位置:首页 > 移动开发 > Android开发

Android Study教你快速实现用户输入手机号或身份证号自动添加空格或者横线

2017-01-12 23:46 861 查看
LZ-Says:貌似过几天就要过年了,学生也渐渐放假回家Happy了,现在还不知道公司什么时候放假,对于回家,愁啊~

前言

今天录入手机号时,突然想起之前某款软件曾经弄得是用户输入时,自动添加空格或者横线,感觉体验性很不错,那么这个该怎么实现呢?自己学习了下,下面为大家讲解~

首先来一张实体图瞅瞅,不然空口套白狼~



程序比较简陋,基本效果实现了,凑合凑合看吧~

分析

首先,这个效果肯定是在用户输入的时候,通过判断输入字符长度,去显示我们想要的状态,这个时候,你就会明白,我们只需要对EditText做输入时监听即可,那么究竟该使用那儿种呢?表急,我们慢慢来~

通过搜索,我们发现Android为我们提供了android.text.TextWatcher类去方便我们管理。下面为大家介绍TextWatcher参数以及相应用法。

TextWatcher接口简介及相关用法

进入TextWatcher类中,我们发现他内部提供了如下三个方法,那么这三个方法都干什么的呢?让我们一起来看看吧~

1) public void beforeTextChanged(CharSequence s, int start,

int count, int after);

- s:这里的s表示改变之前的内容,通常start和count组合,可以在s中读取本次改变字段中被改变的内容

- start:开始的位置

- count:被改变的旧内容数

- after:改变后的内容数量

2) public void onTextChanged(CharSequence s, int start, int before, int count);

- start:开始的位置

- before:被改变的旧内容数

- count:新增后的内容数量

- (我们主要在这里对EditText进行修改)

3) public void afterTextChanged(Editable s);

- 表示改变后新的内容的数量

传统录入与修改后效果相对比

一般的输入手机号显示的格式为:13811604922

但是经过我们修改后就变成了:138 1160 4922或者138-1160-4922

大家仔细看,是不是经过修改之后,用户体验性相对来说变好了呢?

而一般的身份证号输入后显示为:130725199608071212

而经过我们修改后则变成:130 725 1996 0807 1212

是不是感觉一下子好多了呢?

实践才是检验真理的唯一标准

1.编写布局文件

布局文件内容主要为一个TextView提示,以及俩个EditText,方便我们做测试。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
tools:context="com.hlq.textwatcher.MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="本示例Demo主要为大家展示如何在用户输入手机号或者身份证的时候自动添加空格或者-" />

<EditText
android:id="@+id/edt_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="我是手机号"
android:maxLength="13"
android:padding="15dp" />

<EditText
android:id="@+id/edt_idcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="我是身份证号"
android:maxLength="22"
android:padding="15dp" />
</LinearLayout>


2.编写TextWatcher监听类

为了方便后期管理维护拓展,LZ个人将这块单独提取出来,调用时只需要传递当前类型(手机号,身份证号)即可,后期大家可根据相应需求进行拓展,具体代码如下:

package com.hlq.textwatcher.listener;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

/**
* 监听工具类
*
* @author HLQ
* @createtime 2017-1-12
* @remarks 主要为用户手机号以及身份证号添加空格,增加用户体验友好性
*/
public class TextWatcherListener {

private EditText editText;

private TextWatcher watcher;

private static final int TEXT_WATCHER_STATES = 1;
public static final String TEXT_WATCHER_PHONE = "phoneNum";
public static final String TEXT_WATCHER_IDCARD = "idCard";

public TextWatcherListener(String state, EditText editText) {
this.editText = editText;
if (TEXT_WATCHER_PHONE.equals(state)) {
watcher = phoneWatcher;
}
if (TEXT_WATCHER_IDCARD.equals(state)) {
watcher = idCardWatcher;
}
editText.addTextChangedListener(watcher);
}

TextWatcher phoneWatcher = new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == TEXT_WATCHER_STATES) {
int textLength = s.toString().length();
if (textLength == 3 || textLength == 8) {
editText.setText(s + " ");
editText.setSelection(editText.getText().toString().length());
}
}
}

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

@Override
public void afterTextChanged(Editable s) {
}
};

TextWatcher idCardWatcher = new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == TEXT_WATCHER_STATES) {
int textLength = s.toString().length();
if (textLength == 3 || textLength == 7 || textLength == 12 || textLength == 17) {
editText.setText(s + " ");
editText.setSelection(editText.getText().toString().length());
}
}
}

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

@Override
public void afterTextChanged(Editable s) {
}
};

}


3.MainActivity调用

package com.hlq.textwatcher;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

import com.hlq.textwatcher.listener.TextWatcherListener;

public class MainActivity extends Activity {

private EditText edtPhone, edtIdcard;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edtPhone = (EditText) findViewById(R.id.edt_phone);
edtIdcard = (EditText) findViewById(R.id.edt_idcard);

new TextWatcherListener(TextWatcherListener.TEXT_WATCHER_PHONE, edtPhone);

new TextWatcherListener(TextWatcherListener.TEXT_WATCHER_IDCARD, edtIdcard);

}
}


结束语

怎么样,很简单吧,虽然只是一个微不足道的小地方,但是我们去做了,一点点去将我们负责的APP写的更好,一点点积累。未来,一定会有我们的一席之地!!!加油~

下载地址

鉴于某些同志比较懒,特提供访问地址:

亲们,代码上传CSDN上,等好久也没显示,明天再看看。大家有时间可以去我GitHub上看看,代码已上传。

https://github.com/HLQ-Struggle/TextWatchers

2017年8月29日09:41:42更新如下:

在某天浏览GitHub时,发现了一个不错的控件,XEditText,功能比较实用,常用的一些方法什么的都有,下面附上地址链接,大家可以查看,当然也可以直接从我的GitHub上查看。

原文地址:

https://github.com/woxingxiao/XEditText
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android