-
Notifications
You must be signed in to change notification settings - Fork 119
Quick Start
Egor Taflanidi edited this page Aug 29, 2019
·
6 revisions
Given you have already installed our library, let's get started and put together a small project.
Let's say, you've got an EditText
in your XML layout.
First of all, make sure its digits
contains the full set of expected characters for this particular case:
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890+-() "
android:inputType="number" />
Here's a programmatic equivalent using Java:
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setKeyListener(DigitsKeyListener.getInstance("1234567890+-() "));
I wanna know why
Here, our library uses an Editable variable, which accepts only pre-registered characters from the corresponding input type. Specifying the input type is pretty much the only way to set the onscreen keyboard type. Numeric keyboard doesn't allow special characters like "(" or space. Thus, you'll end up with an IndexOutOfBoundsException crash.
Okay, now you've got to instantiate an MaskedTextChangedListener
instance, which is a TextWatcher
and an OnFocusChangeListener
.
Then wire it up with the corresponding EditText
like this:
final EditText editText = findViewById(R.id.edit_text);
final MaskedTextChangedListener listener = MaskedTextChangedListener.Companion.installOn(
editText,
"+7 ([000]) [000]-[00]-[00]",
new MaskedTextChangedListener.ValueListener() {
@Override
public void onTextChanged(boolean maskFilled, @NonNull final String extractedValue, @NonNull final String formattedValue) {
Log.d("TAG", extractedValue);
Log.d("TAG", String.valueOf(maskFilled));
}
}
);
editText.setHint(listener.placeholder());
Alternatively, you may manually subscribe an MaskedTextChangedListener
:
val editText = findViewById(R.id.edit_text) // echo "Hello Kotlin Extensions"
val listener = MaskedTextChangedListener("+7 ([000]) [000]-[00]-[00]", editText)
editText.addTextChangedListener(listener)
editText.onFocusChangeListener = listener
All set. Run.
Sample project is located under the /app
.