我如何设置的EditText只有十六进制数字的输入?(How do I set a EditText

2019-06-24 13:35发布

我把这个代码上的EditText控制,使其只接受十六进制数字。 我该如何去这样做呢?

bin = (EditText)findViewById(R.id.editText02);
hex = (EditText)findViewById(R.id.editText3);
dec = (EditText)findViewById(R.id.editText1);
oct = (EditText)findViewById(R.id.editText04);

Answer 1:

TextWatcher也是不错的选择,但我更喜欢使用自定义过滤器。 因此,简单的方法是使用输入过滤器,并采取对飞每焦炭的控制,请看以下例子,希望这有助于

    import android.text.InputFilter;
    import android.text.InputType;

    EditText input_moodMsg; 
    // initialize this edittext etc etc
    //....
    // here comes the filter to control input on that component
    InputFilter inputFilter_moodMsg = new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end,Spanned dest, int dstart, int dend) {

                    if (source.length()>44) return "";// max 44chars

// Here you can add more controls, e.g. allow only hex chars etc

                    for (int i = start; i < end; i++) { 
                         if (!Character.isLetterOrDigit(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i))
                                 && source.charAt(i)!='-'
                                 && source.charAt(i)!='.'
                                 && source.charAt(i)!='!'
                                 ) { 
                             return "";     
                         }     
                    }
                    return null;   
                }
            };
            input_moodMsg.setFilters(new InputFilter[] { inputFilter_moodMsg });
            input_moodMsg.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);


Answer 2:

在你的Android布局XML文件中添加下列属性的EditText:

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

第一属性只允许与这些数字输入。 任何其他输入被拒绝,不显示。 第二个属性大写字符。

归功于这个博客帖子: http://mobile.antonio081014.com/2012/04/how-to-let-input-of-edittext-only-be.html



Answer 3:

有两种选择,一种是由穆罕默德Annaqeeb使用性质描述:

<EditText
        android:id="@+id/myTextView"
        android:digits="0123456789ABCDEF"
        android:inputType="textCapCharacters"/>

另一个选项是使用输入过滤器和正则表达式只允许十六进制字符:

EditText myTextField = (EditText) findViewById(R.id.myTextView);

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 sb = new StringBuilder();

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

            if (!Character.isLetterOrDigit(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i))                            ) {
                //is not(Letter or Digit or space);
                return "";
            }

            //Only allow characters "0123456789ABCDEF";
            Matcher matcher = patern.matcher(String.valueOf(source.charAt(i)));
            if (!matcher.matches()) {
                return "";
            }

            //Add character to Strinbuilder
            sb.append(source.charAt(i));

            /*counterForSpace++;
            if(counterForSpace>1){
                //Restar counter contador
                counterForSpace = 0;
                //Add space!
                sb.append(" ");
            }*/

        }
        //Return text in UpperCase.
        return  sb.toString().toUpperCase();
    }
};

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

两种方案各有其结果是:

勾选此相关的答案太:

https://es.stackoverflow.com/a/211240/95



文章来源: How do I set a EditText to the input of only hexadecimal numbers?