How to use TextWatcher with EditText
up vote
1
down vote
favorite
I'm trying to implement an app to just accept hexadecimal numbers and every 4 hexadecimal numbers must insert a space, I've managed to do this separately using TextWatcher
:
1.- TextWatcher
only accepts hexadecimal numbers:
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = s.toString();
if (str.isEmpty()) {
mTextoEditor2.append(newStr);
newStr = "";
} else if (!str.equals(newStr)) {
newStr = str.replaceAll("[^A-F0-9a-f]", "");
mTextoEditor2.setText("");
}
}
2.- TextWatcher
inserts a space every 4 characters:
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 4)
{
mTextoEditor2.setText(mTextoEditor2.getText()+" ");
mTextoEditor2.setSelection(mTextoEditor2.getText().length());
}
}
But now, I cannot unite both into one in a single TextWatcher
. Could someone please help me resolve this issue?
textwatcher
add a comment |
up vote
1
down vote
favorite
I'm trying to implement an app to just accept hexadecimal numbers and every 4 hexadecimal numbers must insert a space, I've managed to do this separately using TextWatcher
:
1.- TextWatcher
only accepts hexadecimal numbers:
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = s.toString();
if (str.isEmpty()) {
mTextoEditor2.append(newStr);
newStr = "";
} else if (!str.equals(newStr)) {
newStr = str.replaceAll("[^A-F0-9a-f]", "");
mTextoEditor2.setText("");
}
}
2.- TextWatcher
inserts a space every 4 characters:
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 4)
{
mTextoEditor2.setText(mTextoEditor2.getText()+" ");
mTextoEditor2.setSelection(mTextoEditor2.getText().length());
}
}
But now, I cannot unite both into one in a single TextWatcher
. Could someone please help me resolve this issue?
textwatcher
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to implement an app to just accept hexadecimal numbers and every 4 hexadecimal numbers must insert a space, I've managed to do this separately using TextWatcher
:
1.- TextWatcher
only accepts hexadecimal numbers:
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = s.toString();
if (str.isEmpty()) {
mTextoEditor2.append(newStr);
newStr = "";
} else if (!str.equals(newStr)) {
newStr = str.replaceAll("[^A-F0-9a-f]", "");
mTextoEditor2.setText("");
}
}
2.- TextWatcher
inserts a space every 4 characters:
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 4)
{
mTextoEditor2.setText(mTextoEditor2.getText()+" ");
mTextoEditor2.setSelection(mTextoEditor2.getText().length());
}
}
But now, I cannot unite both into one in a single TextWatcher
. Could someone please help me resolve this issue?
textwatcher
I'm trying to implement an app to just accept hexadecimal numbers and every 4 hexadecimal numbers must insert a space, I've managed to do this separately using TextWatcher
:
1.- TextWatcher
only accepts hexadecimal numbers:
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = s.toString();
if (str.isEmpty()) {
mTextoEditor2.append(newStr);
newStr = "";
} else if (!str.equals(newStr)) {
newStr = str.replaceAll("[^A-F0-9a-f]", "");
mTextoEditor2.setText("");
}
}
2.- TextWatcher
inserts a space every 4 characters:
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 4)
{
mTextoEditor2.setText(mTextoEditor2.getText()+" ");
mTextoEditor2.setSelection(mTextoEditor2.getText().length());
}
}
But now, I cannot unite both into one in a single TextWatcher
. Could someone please help me resolve this issue?
textwatcher
textwatcher
edited Nov 9 at 22:52
Joel
1,6086719
1,6086719
asked Nov 9 at 15:19
W1ll
165
165
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228484%2fhow-to-use-textwatcher-with-edittext%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown