In my app I have to format the input of two EditTexts like:
1234 4567 67: Ten digits that grouped by four. (The space is automatically, not inserted by user)
11/14: Four digits that separated by '/'. (The '/' is inserted automatically)
I don't know how to do it. Please help:
Put a listener on the edit text as afterTextChanged.
Get the number of digits by using the length() function.
Once you get the number of digits, you can extract each digit and then insert space of '/' at the appropriate place.
len=editText.getText.toString().length();
then you can do the appropriate change by checking the length.
num=Integer.parseInt(editText.getText.toString());
temp=num;
if(len>=10)
{
A:
if((len-4)>0)
{
for(i=0;i<(len-4);i++)
{
temp=temp/10; //we get the first 4 digits
}
editText.setText(temp+" "); //place letters and add space
temp=num%(10^(len-4)); //get the num without the first-4 letters
len=len-4; //modify length
goto A; //repeat again
}
editText.setText(temp); //add the last remaining letters
}
else if(len==4)
{
temp=num;
temp=temp%100; //store the last 2 digits
num=num/10; //get the first 2 digits
editText.setText(num+"/"+temp);
}
i havnt tried this but i think this will work.
Hope this will help. :)
I can think of two ways of achieving it:
Use addTextChangedListener:
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Do your tricks here
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Create custom Edittexts
This link wont do what you are looking for, but will give you an idea how to create custom EditText.
Use "onKeyListener" to get the input event.
EditText OnKeyDown
Then check for correct input and count the digits. Add the whitespace/slash in your code.
Sample code:
if (editText.getText.toString().length() % 4 == 0) editText.setText(editText.getText.toString() + " ");
Didn't try it by myself, but this would be the way i would try. In addition i would check for numeric input too.
Related
I have a function that returns some String for letter 'A' when user chooses option 1 and some different String when user chooses option 2:
private String changeText(int option){
if(option==1)
return "Y";
if(option==2)
return "Z";
}
I want to replace the the character in EdittextView when the user selects option 1 and types 'A', replace A with "Y" and same for option 2 with "Z" and this to be done in real-time.
So I came up with TextWatcher.
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0)
return;
s.replace(editText.getSelectionStart(),
editText.getSelectionStart()+1, changeText(option));
}
and it's not working. My guess is that I am trying to replace the character with newer one before it is printed(not sure). I just want to replace the last typed character at any cursor position according to the option selected.
Have you tried
beforeTextChanged(CharSequence s, int start, int count, int after)
in this method you can change symbol before in render. Get CharSequence s and change character at position (start, start+count)
Intro:
I am currently trying to implement an input method for an EditText for my Crossword Puzzle where the user sees something like "____" in the EditText. The underscores mark missing letters, the first char entered will fill the first underscore.
Of course other cells in the puzzle might be solved already, so the EditText text could be "ST_CKOV_RF_OW". I had all this functionality already in my own input view, a subclass of view with an overridden onDraw(). This worked pretty well, except that the view won't appear on some lower Android versions and the Back key slipped through my input routine and wasn't accessible.
So I thought I'd do it with EditText, implement a TextWatcher and be fine, but I can't get it to work properly. What I have right now is working, I can use the keyboard to enter letters, but again the Backspace isn't working, and of course if the user touches into the EditText the position gets messed up.
public void beforeTextChanged(CharSequence s,int start,int count, int after){
et.removeTextChangedListener(textWatcher);
int position = text.indexOf("_");
if(position==-1) onAnswerEntered(et.getText().toString().replace("_", "")); //finished
else {
et.setSelection(et.getText().toString().indexOf("_"));
et.addTextChangedListener(textWatcher);
}
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(textWatcher);
try {
String currentKey = s.toString().substring(start, start+1);
Logger.log("Current Key", currentKey);
int position = text.indexOf("_");
Logger.log("Current Position _ ", position+"");
//replace _ with key
String sbefore=text.substring(0, position);
String safter=text.substring(position+1, text.length());
text=sbefore+currentKey+safter;
int positionNext = text.indexOf("_");
Logger.log("Next Position _ ", positionNext+"");
if(positionNext==-1) onAnswerEntered(et.getText().toString().replace("_","")); //finished
else {
et.setText(text);
et.setSelection(et.getText().toString().indexOf("_"));
et.addTextChangedListener(textWatcher);
}
} catch(IndexOutOfBoundsException ioobe) {
ioobe.printStackTrace();
}
}
I also tried to set an OnKeyListener, but it won't work on EditText (I can get backspace event, nothing else)
So maybe I am totally on the wrong track, but please help me and give me a clue to how I can accomplish my goal. Thanks.
I gave up on it and implemented a simple but working kind of hack. I receive input in my (hidden) EditText now, the output goes to the visible TextView, a function in between fills the "_" with the input from the EditText.
Ex.
hint = "A_A_A_A"
edittext input = "BBB"
textview shows "ABABABA"
I need to control pressed buttons, before they goes to my EditText widget. It's should work like filter.
For example: I need that user could fill EditText only with digits 1,2,3,4,5 other symbols must be ignored. So the part of buttons on virtual keyboard should be disabled or I need to catch last pressed symbol, analyze it and disable for EditText.
Who knows the way how to solve this problem?
Thanks..
statusEdt.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do stuff
charTxt.setText(statusEdt.getText().length() + "/140");
}
});
I used this TextChangedListener(TextWatcher) to keep a count of how many characters had been typed into an EditText for a Twitter client I made. You could probably use a listener like this. You'll want to override beforeTextChanged or onTextChanged. These methods will pass you whatever CharSequence has been typed. You can check what was typed in and if it is not valid input you can remove it by calling setText() and passing in whatever has been typed so far minus the invalid characters.
What you probably need is an InputFilter.
For example to allow only digits, sign and decimal point:
editText.setFilters(DigistKeyListener.getInstance(true, true));
I am working on softkeyboard.
My issues are below.
How to get current position of cursor in text(EditText).
How to get total length of value in text(EditText).
If EditText is multi-line then get current line of cursor in text(EditText).
If you want see my code then see this softkeyboard's link. I am following this code.
You should put textwatcher event in edittext this is the event is execute when user type a character (any in put by key board).
In your case when user type a single character in edittext you got hole text then get length of this text it is your cursor position and total length of value in text.
according to your third question you have all the text written in edit text using above method then you convert all the text in ascii value then compare every character with 13(it is the ascii value of enter in keyboard )and increase counter of line when it condition true using this you find no of line in edit text. i am giving a example for you how to put text watcher in edittext you change in this code and convert it according to your condition.
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = ed.getText().length();
);
}
});
I want to cause the focus of one edit text box to move to another on editting (meaning you can only type on letter before it automatically moves on to the next edit text).
It's the "on edit" that I can't get my head around. Can anyone help me out with a simple example? Theres a lot I need to implement it into, so just a basic understanding should set the ball rolling ^_^
I do not really recommend this. With soft keyboards and multiple languages, what exactly is "one letter"? After all, a soft keyboard might enter in an entire word, like it or not.
CommonsWare makes an excellent point: you can't prevent the user from adding more characters to the EditText box, however you can listen to what's changed and act on that. Here's how to:
EditText editbox = (EditText) findViewById(R.id.MyEditBoxName);
editbox.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
// Test s for length, request focus for the next edit.
// editbox2.requestFocus();
}
});
Be careful not to get yourself into an infinite loop changing the editbox, any changes you make will cause these methods to be called again recursively.