EditText - 如何在组或块中分隔在EditText中键入的字符

时间:2017-03-27 10:57:43

标签: android android-edittext

有没有办法在用户输入时可以在块中对EditText中的字符进行分组?例如:1234 4567 7890等等?

我有一个支持数字编号的编辑文本,并且有16个字符长度,我想将它们分成不同的块,以便更好地查看。

6 个答案:

答案 0 :(得分:2)

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText mEditText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = (EditText) findViewById(R.id.edittext);
        mEditText.addTextChangedListener(new FourDigitFormater());
    }

    class FourDigitFormater implements TextWatcher {

        // Change this to what you want... ' ', '-' etc..
        private static final char space = ' ';
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            // Remove spacing char
            if (s.length() > 0 && (s.length() % 5) == 0) {
                final char c = s.charAt(s.length() - 1);
                if (space == c) {
                    s.delete(s.length() - 1, s.length());
                }
            }
            // Insert char where needed.
            if (s.length() > 0 && (s.length() % 5) == 0) {
                char c = s.charAt(s.length() - 1);
                // Only if its a digit where there should be a space we insert a space
                if (Character.isDigit(c) && TextUtils.split(s.toString(), String.valueOf(space)).length <= 3) {
                    s.insert(s.length() - 1, String.valueOf(space));
                }
            }
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="crxpress.dgflick.com.stackoverflow.MainActivity">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:inputType="phone"/>
</RelativeLayout>

答案 1 :(得分:2)

    editText.addTextChangedListener(new FourDigitCardFormatWatcher(editText));

每4位空格。使用以下代码。

public class FourDigitCardFormatWatcher implements TextWatcher {

    // Change this to what you want... ' ', '-' etc..
        private final String space = " ";
        EditText et_filed;


        public FourDigitCardFormatWatcher(EditText et_filed){
            this.et_filed = et_filed;
        }

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

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String initial = s.toString();
            // remove all non-digits characters
            String processed = initial.replaceAll("\\D", "");

            if (processed.length() > 4){
                processed = processed.replaceAll("(\\d{4})(?=\\d)", "$1 ");
            }

            //Remove the listener
            et_filed.removeTextChangedListener(this);

            //Assign processed text
            et_filed.setText(processed);

            try {
                et_filed.setSelection(processed.length());
        } catch (Exception e) {
            // TODO: handle exception
        }

        //Give back the listener
            et_filed.addTextChangedListener(this);
        }
    }

答案 2 :(得分:1)

使用TextListener,使用返回数组的Arrays.toString(bitstream.split("(?<=\\G.{4})")))解析输入,然后将其设置为EditText

示例:

editText.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) {
            String formatted = Arrays.toString(s.toString().split("(?<=\\G.{4})"));
            // remove listener to avoid cyclic calling
            editText.addTextChangedListener(null);
            editText.setText(formatted);
            // reattach listener
            editText.addTextChangedListener(this);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

答案 3 :(得分:1)

您可以使用TextWatcher。例如:

// we make a filter to accept numbers and space only!
    yourEditText.setFilters(new InputFilter[]{
            new InputFilter() {
                @Override
                public CharSequence filter(CharSequence cs, int start,
                                           int end, Spanned spanned, int dStart, int dEnd) {

                    if (cs.equals("")) { // for backspace
                        return cs;
                    }
                    if (cs.toString().matches("[0-9 ]+")) {
                        return cs;
                    }
                    return "";
                }
            }
    });
    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) {

            if (before == 0) {  // If we aren't deleting characters...
                if (start + count == 4 || start + count == 9 || start + count == 14 || start + count == 19) {
                    // we add a simple space to better looks
                    yourEditText.append(" ");
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            // nothing here
        }
    });

答案 4 :(得分:1)

如果您想视觉上分组,但又不想更改EditText加空格的值,则可以使用以下Span方法:

EditText editText = findViewById(R.id.editText);
editText.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 editable) {
        Object[] paddingSpans = editable.getSpans(0, editable.length(), SpaceSpan.class);
        for (Object span : paddingSpans) {
            editable.removeSpan(span);
        }

        addSpans(editable);
    }

    private static final int GROUP_SIZE = 4;

    private void addSpans(Editable editable) {

        final int length = editable.length();
        for (int i = 1; i * (GROUP_SIZE) < length; i++) {
            int index = i * GROUP_SIZE;
            editable.setSpan(new SpaceSpan(), index - 1, index,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
});

SpaceSpan类如下所示:

/**
 * A {@link ReplacementSpan} used for spacing in {@link android.widget.EditText}
 * to space things out. Adds ' 's
 */
public class SpaceSpan extends ReplacementSpan {

    @Override
    public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) {
        float padding = paint.measureText(" ", 0, 1);
        float textSize = paint.measureText(text, start, end);
        return (int) (padding + textSize);
    }

    @Override
    public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
                     int bottom, @NonNull Paint paint) {
        canvas.drawText(text.subSequence(start, end) + " ", x, y, paint);
    }
}

完整的可用示例here

答案 5 :(得分:1)

//在每个地方都用您的EditText替换EditText ...

EditText.addTextChangedListener(new TextWatcher() {
                byte count1 = 0;
                byte count2 = 0;
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    count1 = (byte)s.length();


                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    count2 = (byte)s.length();
                }

                @Override
                public void afterTextChanged(Editable s) {


                        String initial = EditText.getText().toString(); 

                        String formatted = "";
                        String preFormat = initial.replaceAll("-", "");

                        if (preFormat.length() % 4 == 0 && (count2 - count1 > 0)) {

                            for (int i = 0; i < preFormat.length(); i++) {
                                formatted += preFormat.charAt(i);
                            if (((i + 1) % 4) == 0)
                                    formatted += "-";


                            }
                           EditText.removeTextChangedListener(this);
                            EditText.setText(formatted);
                            EditText.setSelection(formatted.length());
                            EditText.addTextChangedListener(this);


                        }

                }
            });