17

我将在此代码中放入 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);
4

3 回答 3

49

在您的 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

于 2013-06-27T23:42:38.287 回答
4

有两种选择,一种是 Muhammad Annaqeeb 使用属性描述的:

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

其他选项将使用InputFilter和 REGEX 只允许十六进制字符:

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

水图像介绍

于 2018-11-06T22:28:43.323 回答
3

TextWatcher 也是不错的选择,但我更喜欢使用自定义过滤器。因此,更简单的方法是使用 InputFilter 并即时控制每个字符,请参见下面的示例,希望这会有所帮助

    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);
于 2012-05-18T08:18:52.127 回答