可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Android - I want to get a number input from the user into an EditText - it needs to be separated by spaces - every 4 characters.
Example: 123456781234 -> 1234 5678 1234
This is only for visual purpose. However i need the string without spaces for further usage.
What is the easiest way I can do this?
回答1:
You need to use TextWatcher to achieve visual purpose spaces.
And use any simply split string by space logic to join it back or loop through the entire string per character wise and eliminate (char) 32
from the string
回答2:
is this editext for credit card?
first create count variable
int count = 0;
then put this in your oncreate(activity) / onviewcreated(fragment)
ccEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) { /*Empty*/}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) { /*Empty*/ }
@Override
public void afterTextChanged(Editable s) {
int inputlength = ccEditText.getText().toString().length();
if (count <= inputlength && inputlength == 4 ||
inputlength == 9 || inputlength == 14)){
ccEditText.setText(ccEditText.getText().toString()+" ");
int pos = ccEditText.getText().length();
ccEditText.setSelection(pos);
} else if(count >= inputLength &&(inputLength == 4 ||
inputLength == 9 || inputLength == 14)){
ccEditText.setText(ccEditText.getText().toString()
.substring(0,ccEditText.getText()
.toString().length()-1));
int pos = ccEditText.getText().length();
ccEditText.setSelection(pos);
}
count = ccEditText.getText().toString().length();
}
});
回答3:
as @waqas pointed out, you'll need to use a TextWatcher if your aim is to make this happen as the user types the number. Here is one potential way you could achieve the spaces:
StringBuilder s;
s = new StringBuilder(yourTxtView.getText().toString());
for(int i = 4; i < s.length(); i += 5){
s.insert(i, " ");
}
yourTxtView.setText(s.toString());
Whenever you need to get the String without spaces do this:
String str = yourTxtView.getText().toString().replace(" ", "");
回答4:
Format of text is 000 000 0000
android edittext textwatcher format phone number like xxx-xxx-xx-xx
public class PhoneNumberTextWatcher implements TextWatcher {
private static final String TAG = PhoneNumberTextWatcher.class
.getSimpleName();
private EditText edTxt;
private boolean isDelete;
public PhoneNumberTextWatcher(EditText edTxtPhone) {
this.edTxt = edTxtPhone;
edTxt.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
isDelete = true;
}
return false;
}
});
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
if (isDelete) {
isDelete = false;
return;
}
String val = s.toString();
String a = "";
String b = "";
String c = "";
if (val != null && val.length() > 0) {
val = val.replace(" ", "");
if (val.length() >= 3) {
a = val.substring(0, 3);
} else if (val.length() < 3) {
a = val.substring(0, val.length());
}
if (val.length() >= 6) {
b = val.substring(3, 6);
c = val.substring(6, val.length());
} else if (val.length() > 3 && val.length() < 6) {
b = val.substring(3, val.length());
}
StringBuffer stringBuffer = new StringBuffer();
if (a != null && a.length() > 0) {
stringBuffer.append(a);
if (a.length() == 3) {
stringBuffer.append(" ");
}
}
if (b != null && b.length() > 0) {
stringBuffer.append(b);
if (b.length() == 3) {
stringBuffer.append(" ");
}
}
if (c != null && c.length() > 0) {
stringBuffer.append(c);
}
edTxt.removeTextChangedListener(this);
edTxt.setText(stringBuffer.toString());
edTxt.setSelection(edTxt.getText().toString().length());
edTxt.addTextChangedListener(this);
} else {
edTxt.removeTextChangedListener(this);
edTxt.setText("");
edTxt.addTextChangedListener(this);
}
}
}
回答5:
I have created a class that encapsulates the given behavior.
/**
* Custom [TextWatcher] class that appends a given [separator] for every [interval].
*/
abstract class SeparatorTextWatcher(
private val separator: Char,
private val interval: Int
) : TextWatcher {
private var dirty = false
private var isDelete = false
override fun afterTextChanged(editable: Editable?) {
if (dirty) return
dirty = true
val text = editable.toString().handleSeparator()
onAfterTextChanged(text)
dirty = false
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Empty
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
isDelete = before != 0
}
private fun String.handleSeparator(): String {
val stringBuilder = StringBuilder(this)
if (length > 0 && length.rem(interval + 1) == 0) {
if (isDelete) {
stringBuilder.deleteCharAt(length - 1)
} else {
stringBuilder.insert(length - 1, separator)
}
}
return stringBuilder.toString()
}
/**
* Subclasses must implement this method to get the formatted text.
*/
abstract fun onAfterTextChanged(text: String)
}
Here's a snippet on how to use it:
editText.addTextChangedListener(object : SeparatorTextWatcher(' ', 4) {
override fun onAfterTextChanged(text: String) {
editText.run {
setText(text)
setSelection(text.length)
}
}
})
回答6:
Here is a little help function. For your example you would call it with
addPadding(" ", "123456781234", 4);
/**
* @brief Insert arbitrary string at regular interval into another string
*
* @param t String to insert every 'num' characters
* @param s String to format
* @param num Group size
* @return
*/
private String addPadding(String t, String s, int num) {
StringBuilder retVal;
if (null == s || 0 >= num) {
throw new IllegalArgumentException("Don't be silly");
}
if (s.length() <= num) {
//String to small, do nothing
return s;
}
retVal = new StringBuilder(s);
for(int i = retVal.length(); i > 0; i -= num){
retVal.insert(i, t);
}
return retVal.toString();
}
回答7:
change the live text while typing is some what difficult. we should handle the following issues.
a. cursor position
b. we should allow the user delete the entered text.
The following code handle both the issues.
Add TextWatcher to EditText, and get the text from "afterTextchanged()" and write your logic
String str="";
int strOldlen=0;
@Override
public void afterTextChanged(Editable s) {
str = edtAadharNumber.getText().toString();
int strLen = str.length();
if(strOldlen<strLen) {
if (strLen > 0) {
if (strLen == 4 || strLen == 9) {
str=str+" ";
edtAadharNumber.setText(str);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}else{
if(strLen==5){
if(!str.contains(" ")){
String tempStr=str.substring(0,strLen-1);
tempStr +=" "+str.substring(strLen-1,strLen);
edtAadharNumber.setText(tempStr);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}
}
if(strLen==10){
if(str.lastIndexOf(" ")!=9){
String tempStr=str.substring(0,strLen-1);
tempStr +=" "+str.substring(strLen-1,strLen);
edtAadharNumber.setText(tempStr);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}
}
strOldlen = strLen;
}
}else{
return;
}
}else{
strOldlen = strLen;
Log.i("MainActivity ","keyDel is Pressed ::: strLen : "+strLen+"\n old Str Len : "+strOldlen);
}
}
}
Here I am trying to add space for every four characters. After adding first space, then the length of the text is 5. so next space is after 9 characters like that.
if (strLen== 4||strLen==9)
- Here another problem is cursor position, once you modify the text of the edittext, the cursor move to first place. so we need to set the cursor manually.
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
- My text length is only 12 characters. So I am doing manual calculations, if your text is dynamic then you write dynamic logic.
回答8:
cleaner version of @Ario's answer which follows the DRY principle:
private int prevCount = 0;
private boolean isAtSpaceDelimiter(int currCount) {
return currCount == 4 || currCount == 9 || currCount == 14;
}
private boolean shouldIncrementOrDecrement(int currCount, boolean shouldIncrement) {
if (shouldIncrement) {
return prevCount <= currCount && isAtSpaceDelimiter(currCount);
} else {
return prevCount > currCount && isAtSpaceDelimiter(currCount);
}
}
private void appendOrStrip(String field, boolean shouldAppend) {
StringBuilder sb = new StringBuilder(field);
if (shouldAppend) {
sb.append(" ");
} else {
sb.setLength(sb.length() - 1);
}
cardNumber.setText(sb.toString());
cardNumber.setSelection(sb.length());
}
ccEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String field = editable.toString();
int currCount = field.length();
if (shouldIncrementOrDecrement(currCount, true)){
appendOrStrip(field, true);
} else if (shouldIncrementOrDecrement(currCount, false)) {
appendOrStrip(field, false);
}
prevCount = cardNumber.getText().toString().length();
}
});
回答9:
Simple Answer
YourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int len=s.toString().length();
if (before == 0 && (len == 4 || len == 9 || len == 14 ))
YourEditText.append(" ");
}
@Override
public void afterTextChanged(Editable s) {
}
});
回答10:
Assuming that you know the final length of the String, you could implement a TextWatcher
this way:
override fun setUp(view: View?) {
editText.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {
if(p2 == 0 && (p0.length == 4 || p0.length == 9 || p0.length == 14))
editText.append(" ")
}
override fun afterTextChanged(p0: Editable?) {
}
})
You just add a space each 4-digits block. p2 == 0
is to assure the user is not deleting, otherwise he/she would get stock.
The code is in Kotlin, You can do it exactly the same way in Java.