Last active
March 29, 2018 10:32
-
-
Save aartikov/92deb0270d451032892d9e88fe6786dd to your computer and use it in GitHub Desktop.
Text input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import android.text.InputFilter; | |
import android.text.Spanned; | |
public class RegexInputFilter implements InputFilter { | |
private Pattern mPattern; | |
public RegexInputFilter(String pattern) { | |
mPattern = Pattern.compile(pattern); | |
} | |
@Override | |
public CharSequence filter(CharSequence source, int sourceStart, int sourceEnd, Spanned destination, int destinationStart, int destinationEnd) { | |
String textToCheck = destination.subSequence(0, destinationStart).toString() | |
+ source.subSequence(sourceStart, sourceEnd) | |
+ destination.subSequence(destinationEnd, destination.length()).toString(); | |
Matcher matcher = mPattern.matcher(textToCheck); | |
if (!matcher.matches() && !matcher.hitEnd()) { | |
return ""; | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.v7.widget.AppCompatEditText; | |
import android.text.Editable; | |
import android.text.InputFilter; | |
import android.text.InputType; | |
import android.text.method.DigitsKeyListener; | |
import android.util.AttributeSet; | |
public class UserIdEditText extends AppCompatEditText { | |
public static final int MINIMAL_USER_ID_LENGTH = 10; | |
public UserIdEditText(Context context) { | |
super(context); | |
} | |
public UserIdEditText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public UserIdEditText(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
setInputType(InputType.TYPE_CLASS_NUMBER); | |
setKeyListener(DigitsKeyListener.getInstance("0123456789-")); | |
setFilters(new InputFilter[]{ | |
new RegexInputFilter("\\d{3}-\\d{6,7}") | |
}); | |
addTextChangedListener(new TextWatcherAdapter() { | |
int mPreviousLength; | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
mPreviousLength = s.length(); | |
} | |
@Override | |
public void afterTextChanged(Editable text) { | |
if (mPreviousLength < 3 && text.length() == 3) { | |
text.append('-'); | |
} else if (mPreviousLength > 3 && text.length() == 3) { | |
text.delete(2, 3); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment