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.editText02);
hex = (EditText)findViewById(R.id.editText3);
dec = (EditText)findViewById(R.id.editText1);
oct = (EditText)findViewById(R.id.editText04);
TextWatcher is also good option, however I prefer using custom filters.
thereby, simpler way is to use InputFilter and take control of every char on the fly, see example below, hope this helps
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);
In your android layout XML file add the following attributes to EditText:
<EditText
android:digits="0123456789ABCDEF"
android:inputType="textCapCharacters"
/>
The first attribute allows only input with these numerals. Any other input is rejected and not displayed. The second attribute capitalizes characters.
credit goes to this blog post:
http://mobile.antonio081014.com/2012/04/how-to-let-input-of-edittext-only-be.html
There are two options, one is described by Muhammad Annaqeeb using the properties:
<EditText
android:id="@+id/myTextView"
android:digits="0123456789ABCDEF"
android:inputType="textCapCharacters"/>
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