Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First commit

  • Loading branch information...
commit 3952b845deb041b0fc0d0ee284f26f2b89ac6819 1 parent 27c9378
Pietro Caselani authored
35 Example/.gitignore
... ... @@ -0,0 +1,35 @@
  1 +#Android generated
  2 +bin
  3 +gen
  4 +lint.xml
  5 +
  6 +#Eclipse
  7 +.project
  8 +.classpath
  9 +.settings
  10 +.checkstyle
  11 +
  12 +#IntelliJ IDEA
  13 +.idea
  14 +*.iml
  15 +*.ipr
  16 +*.iws
  17 +classes
  18 +gen-external-apklibs
  19 +
  20 +#Maven
  21 +target
  22 +release.properties
  23 +pom.xml.*
  24 +
  25 +#Ant
  26 +build.xml
  27 +ant.properties
  28 +local.properties
  29 +proguard.cfg
  30 +proguard-project.txt
  31 +
  32 +#Other
  33 +.DS_Store
  34 +tmp
  35 +out
16 Example/AndroidManifest.xml
... ... @@ -0,0 +1,16 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3 + package="com.pc.edittext.example"
  4 + android:versionCode="1"
  5 + android:versionName="1.0">
  6 + <uses-sdk android:minSdkVersion="4"/>
  7 + <application android:label="@string/app_name">
  8 + <activity android:name="MyActivity"
  9 + android:label="@string/app_name">
  10 + <intent-filter>
  11 + <action android:name="android.intent.action.MAIN"/>
  12 + <category android:name="android.intent.category.LAUNCHER"/>
  13 + </intent-filter>
  14 + </activity>
  15 + </application>
  16 +</manifest>
15 Example/project.properties
... ... @@ -0,0 +1,15 @@
  1 +# This file is automatically generated by Android Tools.
  2 +# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
  3 +#
  4 +# This file must be checked in Version Control Systems.
  5 +#
  6 +# To customize properties used by the Ant build system edit
  7 +# "ant.properties", and override values to adapt the script to your
  8 +# project structure.
  9 +#
  10 +# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
  11 +#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
  12 +
  13 +# Project target.
  14 +target=android-4
  15 +android.library.reference.1=../MaskedEditText
44 Example/res/layout/main.xml
... ... @@ -0,0 +1,44 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:orientation="vertical"
  4 + android:layout_width="fill_parent"
  5 + android:layout_height="fill_parent"
  6 + >
  7 + <EditText
  8 + android:id="@+id/editTextCEP"
  9 + android:layout_width="fill_parent"
  10 + android:layout_height="wrap_content"
  11 + android:hint="@string/cep"
  12 + />
  13 + <EditText
  14 + android:id="@+id/editTextPhone"
  15 + android:layout_width="fill_parent"
  16 + android:layout_height="wrap_content"
  17 + android:hint="@string/phone"
  18 + />
  19 + <EditText
  20 + android:id="@+id/editTextCNPJ"
  21 + android:layout_height="wrap_content"
  22 + android:layout_width="fill_parent"
  23 + android:hint="@string/cnpj"
  24 + />
  25 + <EditText
  26 + android:id="@+id/editTextCPF"
  27 + android:layout_height="wrap_content"
  28 + android:layout_width="fill_parent"
  29 + android:hint="@string/cpf"
  30 + />
  31 + <EditText
  32 + android:id="@+id/editTextRG"
  33 + android:layout_width="fill_parent"
  34 + android:layout_height="wrap_content"
  35 + android:hint="@string/rg"
  36 + />
  37 + <EditText
  38 + android:id="@+id/editTextOther"
  39 + android:layout_width="fill_parent"
  40 + android:layout_height="wrap_content"
  41 + android:hint="@string/other"
  42 + />
  43 +</LinearLayout>
  44 +
10 Example/res/values/strings.xml
... ... @@ -0,0 +1,10 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<resources>
  3 + <string name="app_name">Example</string>
  4 + <string name="cep">CEP</string>
  5 + <string name="phone">Fone</string>
  6 + <string name="cnpj">CNPJ</string>
  7 + <string name="cpf">CPF</string>
  8 + <string name="rg">RG</string>
  9 + <string name="other">Outro</string>
  10 +</resources>
41 Example/src/com/pc/edittext/example/MyActivity.java
... ... @@ -0,0 +1,41 @@
  1 +package com.pc.edittext.example;
  2 +
  3 +import android.app.Activity;
  4 +import android.os.Bundle;
  5 +import android.widget.EditText;
  6 +import com.pc.edittext.MaskedWatcher;
  7 +
  8 +public class MyActivity extends Activity {
  9 + /**
  10 + * Called when the activity is first created.
  11 + */
  12 + @Override
  13 + public void onCreate(Bundle savedInstanceState) {
  14 + super.onCreate(savedInstanceState);
  15 + setContentView(R.layout.main);
  16 +
  17 + EditText editTextCEP = (EditText) findViewById(R.id.editTextCEP);
  18 + EditText editTextPhone = (EditText) findViewById(R.id.editTextPhone);
  19 + EditText editTextCNPJ = (EditText) findViewById(R.id.editTextCNPJ);
  20 + EditText editTextCPF = (EditText) findViewById(R.id.editTextCPF);
  21 + EditText editTextRG = (EditText) findViewById(R.id.editTextRG);
  22 + EditText editTextOther = (EditText) findViewById(R.id.editTextOther);
  23 +
  24 + MaskedWatcher cepWatcher = new MaskedWatcher("#####-###", editTextCEP);
  25 + cepWatcher.setAcceptOnlyNumbers(true);
  26 +
  27 + MaskedWatcher phoneWatcher = new MaskedWatcher("(##) ####-####", editTextPhone);
  28 + phoneWatcher.setAcceptOnlyNumbers(true);
  29 +
  30 + MaskedWatcher cnpjWatcher = new MaskedWatcher("##.###.###/####-##", editTextCNPJ);
  31 + cnpjWatcher.setAcceptOnlyNumbers(true);
  32 +
  33 + new MaskedWatcher("###.###.###-##", editTextCPF).setAcceptOnlyNumbers(true);
  34 +
  35 + new MaskedWatcher("##########", editTextRG).setAcceptOnlyNumbers(true);
  36 +
  37 + new MaskedWatcher("## ## -- ## () [##]", editTextOther);
  38 +
  39 +
  40 + }
  41 +}
35 MaskedEditText/.gitignore
... ... @@ -0,0 +1,35 @@
  1 +#Android generated
  2 +bin
  3 +gen
  4 +lint.xml
  5 +
  6 +#Eclipse
  7 +.project
  8 +.classpath
  9 +.settings
  10 +.checkstyle
  11 +
  12 +#IntelliJ IDEA
  13 +.idea
  14 +*.iml
  15 +*.ipr
  16 +*.iws
  17 +classes
  18 +gen-external-apklibs
  19 +
  20 +#Maven
  21 +target
  22 +release.properties
  23 +pom.xml.*
  24 +
  25 +#Ant
  26 +build.xml
  27 +ant.properties
  28 +local.properties
  29 +proguard.cfg
  30 +proguard-project.txt
  31 +
  32 +#Other
  33 +.DS_Store
  34 +tmp
  35 +out
7 MaskedEditText/AndroidManifest.xml
... ... @@ -0,0 +1,7 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3 + package="com.pc.edittext"
  4 + android:versionCode="1"
  5 + android:versionName="1.0">
  6 +
  7 +</manifest>
15 MaskedEditText/project.properties
... ... @@ -0,0 +1,15 @@
  1 +# This file is automatically generated by Android Tools.
  2 +# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
  3 +#
  4 +# This file must be checked in Version Control Systems.
  5 +#
  6 +# To customize properties used by the Ant build system edit
  7 +# "ant.properties", and override values to adapt the script to your
  8 +# project structure.
  9 +#
  10 +# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
  11 +#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
  12 +
  13 +android.library=true
  14 +# Project target.
  15 +target=android-4
87 MaskedEditText/src/com/pc/edittext/MaskedWatcher.java
... ... @@ -0,0 +1,87 @@
  1 +package com.pc.edittext;
  2 +
  3 +import android.text.Editable;
  4 +import android.text.SpannableStringBuilder;
  5 +import android.text.TextWatcher;
  6 +import android.widget.EditText;
  7 +
  8 +/**
  9 + * Created by Pietro Caselani
  10 + */
  11 +public class MaskedWatcher implements TextWatcher {
  12 + private String mMask;
  13 + private boolean mIsUpdating, mAcceptOnlyNumbers;
  14 + private EditText mEditText;
  15 +
  16 + public MaskedWatcher(String mask, EditText editText) {
  17 + if (mask == null)
  18 + throw new RuntimeException("mask parameter from constructor can't be null");
  19 + if (editText == null)
  20 + throw new RuntimeException("editText parameter from constructor can't be null");
  21 + mMask = mask;
  22 + mEditText = editText;
  23 + mIsUpdating = false;
  24 + mEditText.addTextChangedListener(this);
  25 + }
  26 +
  27 + public boolean acceptOnlyNumbers() {
  28 + return mAcceptOnlyNumbers;
  29 + }
  30 +
  31 + public void setAcceptOnlyNumbers(boolean acceptOnlyNumbers) {
  32 + mAcceptOnlyNumbers = acceptOnlyNumbers;
  33 + }
  34 +
  35 + @Override
  36 + public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  37 + }
  38 +
  39 + @Override
  40 + public void onTextChanged(CharSequence s, int start, int before, int count) {
  41 + if (mIsUpdating) {
  42 + mIsUpdating = false;
  43 + return;
  44 + }
  45 +
  46 + Editable editable = mEditText.getText();
  47 + String string = s.toString();
  48 + int end = count + start;
  49 + String inserted = string.substring(start, end);
  50 +
  51 + if (mAcceptOnlyNumbers && !inserted.matches("[0-9]*") && !string.equalsIgnoreCase("")) {
  52 + mIsUpdating = true;
  53 + editable.delete(start, end);
  54 + //TODO
  55 + // I need a solution to not set text "again". Changes keyboard and selection
  56 + mIsUpdating = true;
  57 + mEditText.setText(mEditText.getText());
  58 + mEditText.setSelection(end - 1);
  59 + return;
  60 + }
  61 +
  62 + int currentLength = s.length(), maxLength = mMask.length();
  63 + if (currentLength > maxLength) {
  64 + mIsUpdating = true;
  65 + editable.delete(maxLength, currentLength);
  66 + //TODO
  67 + // I need a solution to not set text "again". Changes keyboard and selection
  68 + mIsUpdating = true;
  69 + mEditText.setText(mEditText.getText());
  70 + mEditText.setSelection(maxLength);
  71 + return;
  72 + }
  73 +
  74 + for (int i = start; i < editable.length(); i++) {
  75 + char c = mMask.charAt(i);
  76 + char editableC = editable.charAt(i);
  77 + if (c != '#' && c != editableC) {
  78 + mIsUpdating = true;
  79 + editable.insert(i, String.valueOf(c));
  80 + }
  81 + }
  82 + }
  83 +
  84 + @Override
  85 + public void afterTextChanged(Editable s) {
  86 + }
  87 +}

0 comments on commit 3952b84

Please sign in to comment.
Something went wrong with that request. Please try again.