I was making an application to act as remote keyboard where user will click on an edittext and type and corresponding alphabets will b typed in computer.
I have detected variaous alphabets and numbers with the help of TextWatcher and sent them to my server successfully.
Problem comes when user presses enter key. This also triggers TextWatcher . and since i am sending the latest entered changes , error shows up on server side.
As a solution what i did is , set one onkeylistener as well which will detect the enter key and perform action and CONSUME it , but unfortunately in that case also first textwatcher gets triggered and then onkeylistener.
Here is the code of my onkeylistener
keyboard.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER&&event.getAction()==KeyEvent.ACTION_UP){
out.println("enter");
return true;
}
return false;
}
});
Code of TextWatcher :
private TextWatcher watcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count>0){
out.println(":"+s.subSequence(start, start+count).toString());
}
}
.
.
.
Problem is on pressing enter key it fires TextWatcher's onTextChanged as well as onkeylistener. whereas i want only on key listener to fire.
I want to fire Textwatcher only in case of alphabets , numbers , some symbols etc.
Also if you can suggest different approach for detecting and sending characters ( soft ) will be great .
Ok i think i solved it. in Textwatcher add this if statement:
if(s.toString().substring(start).contains("\n"))
in that way if last key entered was 'enter' then it would go into this is and then u can perform whatever u want.
Related
I'm using addTextChangedListener(myTextWatcher) to my editText for some currency format.
There is setText() in afterTextChanged in my TextWatcher.
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
(snipped)
et.setText(someText);
(snipped)
et.addTextChangedListener(this);
}
The problem is that even though holding backspace I can delete only one character. I found that setText in the afterTextChanged makes holding backspace not working.
I don't know the exact mechanism but it seems setText push up backspace.
In addition it seems this doesn't happen in Android 7.0 but happens android 8.0 and 9.0.
Thank you in advance!
Use this to know when the backspace is pressed:
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
if(keyCode == KeyEvent.KEYCODE_DEL) {
editText.setText(editText.getText().tostring()); // gets the current text and sets it again
}
return false;
}
});
You said it works only once. By doing this if you press it it gives you another chance by setting text again and then you can press backspace only once again.
i'm working with a bluetooth barcode scanner acting like a bluetooth keyboard, i want to handle the keys when my user scan something but i know he can focus any EditText in the activity so i want to detect which keyboard he uses in my KeyEvent like this :
if KeyEvent.getDevice().getName().equals("Datalogic Scanner") //do stuff
else //let the edittext add the letter
and prevent the EditText to be edited and instead store and process the values the scanner send.
I tried returning true in dispatchKeyEvent in my activity,
I tried returning true in onKeyDown in my activity,
I tried returning true in a OnKeyListener on my EditText and overriding OnKeyDown on my EditText class, but nothing works, the text still get inserted in my EditText.
Any idea ?
Try to use the dispatchKeyEvent like that :
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
return scanManager.handleDispatchKeyEvent(event);
}
And in the scanManager class, you should do something like that :
public boolean handleDispatchKeyEvent(KeyEvent event) {
// handle the event
return true;
}
A few questions about android:
Is it possible to replace the keys in default keyboards ? For example, is it possible to replace the dot in the numeric keyboard with a comma ?
I wrote a very simple IME, but I cannot set it to an EditText. What I want is to set one of my EditText to use the IME I wrote by default, not the default LatinIME. Is that possible ? How inputMethod attribute works ? I set the fully qualified class name of IME but it raises class not found exception.
Thanks.
Is it possible to replace the keys in default keyboards ?
You don't. Users are in control over their device, including what keyboard gets used.
But You can try to make some input methods
Read this tutorial: Creating an Input Method
clone this repo: LatinIME
And if replacing one character is your requirement, you can override text change listener of edittext, and check each entered character and if user entered dot then replace that with comma as
editText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
//Check if s contains dot and replace it with comma
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
But this method executes each key hit in EditText.
try override this method.return another keyCode and see the reslt
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == someKeyCode) {
//...... button is pressed
}
return super.onKeyDown(keyCode, event);
}
Pretty sure replacing keys in the default keyboard is not possible, you would need to write your own keyboard replacement, much like all the keyboard Apps do.
I want to create calculator which have 2 edit text name input1 and input2 and have spinner which show the operand so I try
input1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
calculate();
return false;
}
});
but it's not on time , I have to press back space 1 time in any edittext to calculate it
I think the problem is "onKey" function
Any ideas?
It sounds like you want the calculations to happen on the fly, and you don't have need for a button.
If that is true, you could attach a TextWatcher to each EditText via the addTextChangedListener(TextWatcher) method. Whenever you receive an event from it, you can call calculate(). In this instance you would probably also want to attach an AdapterView.OnItemSelectedListener to your Spinner.
If you ARE using a button to do your calculations, you would simply have a button, give it an OnClickListener which would call your calculate method.
I have 14 edittext boxes that a user can change at will. When the text is changed in one of them, a softpad key press on the 'enter/next/done' key should re-run a calculation using the new text. I've tried onKey listener but it doesn't work on the soft keyboard, on;y the hard keypad. I've tried a textwatcher like onTextChanged but it reacts and runs the calculation when only a single digit is entered, before the user can input a two or more digit number. So... I hear the onEditorActionListener works on soft keypads to watch for the keypress, but I can't get the syntax right. Here's what I have:
In the onCreate method:
myEdittext1.setOnEditorActionListener(this);
...
myEdittext14.setOnEditorActionListener(this);
Then, outside the onCreate method, I have:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
{ //do my calcs}
}
return(true);
}
The code gives me the foreced-close business. Any help would be greatly appreciated.
KeyEvent can be null. You need to check for this situation in your listener.
v The view that was clicked.
actionId Identifier of the action. This will be either the identifier you supplied, or EditorInfo.IME_NULL if being called due to the enter key being pressed.
event If triggered by an enter key, this is the event; otherwise, this is null.
onEditorAction is the appropriate way to listen for finish and Done soft keyboard actions.
so here's a working version:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if (event == null || event.getAction()==KeyEvent.ACTION_UP)
{ //do my calcs
return(true);//reset key event for next press
}
}
The enter key returns a 'null' in the event variable, thats why my version at the top of this post crashed.
Nick's code above worked well too, just remember to set the xml property android:imeOptions=actionDone" for all of the edittext boxes.