EditText Hex mode on Android

1

I need to enter only hexadecimal characters to an editText in Android, but these characters must be entered in pairs, after each pair you should automatically insert a space and then the next pair and so on, someone could tell me how to do it

To enter the hex characters I am using this code:

 @Override
 public boolean onEditorAction(TextView v, int actionId,
 KeyEvent event) {
 if (actionId == EditorInfo.IME_ACTION_DONE
  || event.getAction() == KeyEvent.ACTION_DOWN
  && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
  Log.d("", tv.getText().toString());
  return true;
  }
 return false;
 }
});

and the xml layout is like this:

<RelativeLayout 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=".MainActivity" >

<EditText
    android:id="@+id/edittext_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:digits="0123456789ABCDEF"
    android:imeOptions="actionDone"
    android:inputType="textCapCharacters" />

    
asked by W1ll 06.11.2018 в 18:29
source

1 answer

2

One way to allow your EditText to only accept hexadecimal characters is through the properties:

<EditText
    android:id="@+id/myTextView"

    android:digits="0123456789ABCDEF"
    android:inputType="textCapCharacters"/>

This would set your EditText to only receive the characters set in android:digits .

But what you want is that you also add a space every two characters, to do this you can define a InputFilter , where you will evaluate if the character is alphanumeric and using a REGEX if it is included in the characters "0123456789ABCDEF" , when detecting that you have written 2 allowed characters, you will add a space for the following:

  private int counterForSpace = 0;
...
...

    InputFilter inputFilterText = new InputFilter() {

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

            Pattern patern = Pattern.compile("^\p{XDigit}+$");

            //Stringbuilder para almacenar caracteres
            StringBuilder sb = new StringBuilder();

            for (int i = start; i < end; i++) {

                if (!Character.isLetterOrDigit(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i))                            ) {
                    //Caracter no permitido, no escribe caracter;
                    return "";
                }

                //Solo permite caracteres "0123456789ABCDEF";
                Matcher matcher = patern.matcher(String.valueOf(source.charAt(i)));
                if (!matcher.matches()) {
                    return "";
                }

                //Agrega caracter
                sb.append(source.charAt(i));

                counterForSpace++;
                if(counterForSpace>1){
                    //Reinicia contador
                    counterForSpace = 0;
                    //Agrega espacio
                    sb.append(" ");
                }

            }
            //Agrega texto y convierte a mayusculas
            return  sb.toString().toUpperCase();
        }
    };

    myTextField.setFilters(new InputFilter[] { inputFilterText });
    myTextField.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

This way you would get the desired behavior.

    
answered by 06.11.2018 / 18:35
source