I would put in this code a control on EditText so that it only accepts hexadecimal numbers. How do I go about doing this?
bin = (EditText)findViewById(R.id.
There are two options, one is described by Muhammad Annaqeeb using the properties:
other option would be using an InputFilter and REGEX to allow only hexadecimal characters:
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);
both options have as a result:
Check this related answer too:
https://es.stackoverflow.com/a/211240/95