如何将EditText设置为仅输入十六进制数字?

时间:2012-05-18 07:28:01

标签: android android-layout android-intent

我会在此代码中添加一个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);

3 个答案:

答案 0 :(得分:39)

在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

答案 1 :(得分:3)

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

    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);

答案 2 :(得分:0)

有两个选项,其中一个由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);

这两个选项的结果是:

enter image description here

也检查此相关答案:

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

introducir la descripción de la imagen aquí