实时编辑用户输入

时间:2010-11-13 11:17:20

标签: android string android-edittext

是否可以在用户输入数据时自动将字符插入EditText

即。如果用户输入的是123456789012这样的长号码,是否可以在编辑文本框中输入此号码,但每隔4个字符使用短划线?

因此,当您输入上面的数字时,您会看到它在EditText框中输入,但看起来像这样:1234-5678-9012。

目前我有一个应用程序,您可以在其中输入一个长数字,然后按一个按钮,它会为您插入破折号,但我很好奇是否可以在您键入时完成?

非常感谢您的帮助。

6 个答案:

答案 0 :(得分:11)

通过标记android,我认为你正在讨论android editText,所以你可以通过听TextChangedListener来做到这一点,

已编辑:退格

editText.addTextChangedListener(new TextWatcher() {
            int len=0;
            @Override
            public void afterTextChanged(Editable s) { 
                String str = editText.getText().toString(); 
                 if(str.length()==4&& len <str.length()){//len check for backspace 
                    editText.append("-");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

             String str = editText.getText().toString(); 
              len = str.length();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {  
            }


        }); 

答案 1 :(得分:6)

为了解决这个问题,我写了一个类&#34; AutoAddTextWatcher&#34; :

1.自动将文本插入EditText 2.在您设置的位置将文本插入EditText 3.当文本长度大于1时,删除EditText中设置位置的文本。

enter image description here

代码段:

mEditText_birthday.addTextChangedListener(new AutoAddTextWatcher(mEditText_birthday,
            "/",
            new TextWatcher() {},
            4, 6));

AutoAddTextWatcher类

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;


/**
 * Created by henry.chuang on 2016/5/12.
 */
public class AutoAddTextWatcher implements TextWatcher {
    private CharSequence mBeforeTextChanged;
    private TextWatcher mTextWatcher;
    private int[] mArray_pos;
    private EditText mEditText;
    private String mAppentText;

    public AutoAddTextWatcher(EditText editText, String appendText, int... position){
        this.mEditText = editText;
        this.mAppentText = appendText;
        this.mArray_pos = position.clone();
    }
    public AutoAddTextWatcher(EditText editText, String appendText, TextWatcher textWatcher, int... position){
        this(editText, appendText, position);
        this.mTextWatcher = textWatcher;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        mBeforeTextChanged = s.toString();

        if(mTextWatcher != null)
            mTextWatcher.beforeTextChanged(s, start, count, after);

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        for (int i = 0; i < mArray_pos.length; i++) {
            if(((mBeforeTextChanged.length() - mAppentText.length() * i) == (mArray_pos[i] - 1) &&
                    (s.length() - mAppentText.length() * i) == mArray_pos[i])){
                mEditText.append(mAppentText);

                break;
            }

            if(((mBeforeTextChanged.length() - mAppentText.length() * i) == mArray_pos[i] &&
                    (s.length() - mAppentText.length() * i) == (mArray_pos[i] + 1))){
                int idx_start = mArray_pos[i] + mAppentText.length() * i;
                int idx_end = Math.min(idx_start + mAppentText.length(), s.length());

                String sub = mEditText.getText().toString().substring(idx_start,  idx_end);

                if(!sub.equals(mAppentText)){
                    mEditText.getText().insert(s.length() - 1, mAppentText);
                }

                break;
            }

            if(mAppentText.length() > 1 &&
                    (mBeforeTextChanged.length() - mAppentText.length() * i) == (mArray_pos[i] + mAppentText.length()) &&
                    (s.length() - mAppentText.length() * i) == (mArray_pos[i] + mAppentText.length() - 1)){
                int idx_start = mArray_pos[i] + mAppentText.length() * i;
                int idx_end = Math.min(idx_start + mAppentText.length(), s.length());

                mEditText.getText().delete(idx_start, idx_end);

                break;
            }

        }

        if(mTextWatcher != null)
            mTextWatcher.onTextChanged(s, start, before, count);

    }

    @Override
    public void afterTextChanged(Editable s) {
        if(mTextWatcher != null)
            mTextWatcher.afterTextChanged(s);

    }

}

完整的演示来源:
https://github.com/henrychuangtw/AutoInsertEditText

答案 2 :(得分:0)

@Override
public void afterTextChanged(Editable s) {

    if(s.length() == 3 && len < s.length()){
        s.append(" - ");
    }

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    len = s.length();
}

这也可以,只有这段代码会在第3个字符后插入" - "

答案 3 :(得分:0)

这是我用的

private boolean mInEdit;

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (!mInEdit) {
        mInEdit = true;
        String delimiter = " - ";
        //Remove chars from your delimiter first
        String digits = s.toString().replaceAll("[- ]", "")
                .replaceAll("\\d{4}", "$0" + delimiter);
        //Handle deletion
        int dLength = delimiter.length();
        if (before > count && digits.endsWith(delimiter.charAt(dLength - 1)) {
            digits = digits.substring(0, digits.length() - dLength);
        }
        mCardNumber.setText(digits);
        mCardNumber.setSelection(mCardNumber.length());
        mInEdit = false;
    }
}

在这里,您可以将分隔符替换为您想要分隔数字的分隔符。

答案 4 :(得分:0)

对于仍然遇到退格和多个连字符问题的人 -

new TextWatcher() 
{
        boolean hyphenExists;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (s.length() >= 6 && s.charAt(5) == '-') {
                hyphenExists = true;
            } else {
                hyphenExists = false;
            }

            Log.d("TAG", "beforeTextChanged " + s.toString());
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            Log.d("TAG", "onTextChanged " + s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == 5) {
                if (!hyphenExists)
                    s.append('-');
            }
            Log.d("TAG", "afterTextChanged " + s.toString());
        }
    }

答案 5 :(得分:0)

您可以通过更改文字来实现

就我而言,我必须像这样格式化输入:xxx xxx-xxxx

我已按照以下说明进行操作:

etMobileNumber.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {


        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            if (etMobileNumber.text.length == 3 && count != 0) {
                val text = etMobileNumber.getText().toString() + " "
                etMobileNumber.setText(text)
                etMobileNumber.setSelection(text.length)
            } else if (etMobileNumber.text.length == 7 && count != 0) {
                val text = etMobileNumber.getText().toString() + "-"
                etMobileNumber.setText(text)
                etMobileNumber.setSelection(text.length)
            }
        }
    })

输入时,结果在旅途中非常动态。

输入-1234567890 结果-123 456-7890