I have an app with an EditText
and a button named \"forward\". All I want is just when I type \"a\" in the EditText
and click the button, the word \"b\
Tested and works
All letters can be represented by their ASCII values.
If you cast the letters to an int
, add 1, and then cast back to a char
, the letter will increase by 1 ASCII value (the next letter).
For example:
'a'
is 97
'b'
is 98
So if the input was 'a'
and you casted that to an int
, you would get 97
. Then add 1 and get 98
, and then finally cast it back to a char
again and get 'b'
.
Here is an example of casting:
System.out.println( (int)('a') ); // 97
System.out.println( (int)('b') ); // 98
System.out.println( (char)(97) ); // a
System.out.println( (char)(98) ); // b
So, your final code might be this:
// get first char in the input string
char value = et.getText().toString().charAt(0);
int nextValue = (int)value + 1; // find the int value plus 1
char c = (char)nextValue; // convert that to back to a char
et.setText( String.valueOf(c) ); // print the char as a string
Of course this will only work properly if there is one single character as an input.