I need a way to take text from a "EditText" as soon as the user presses "Enter" and it should also call a method if it loses focus.
I created EditText like this:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button2"
android:ems="10" >
To receive key presses your EditText needs to have focus:
editText1.setFocusableInTouchMode(true);
editText1.requestFocus();
then set the onkeylistener():
editText1.setonKeyListener(new OnKeyListener(
#override onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP && keyCode==KeyEvent.KEYCODE_ENTER){
// what you need to do when Enter is pressed
return true;
}else return false;
}
For focus lost, you need to implement the View.onFocusChange interface and check that hasFocus is false
Use an EditText action listener. Refer to this question, it's probably the same: Handle Enter in EditText
For detecting loss in focus:
if(!(et.isFocused)){
//call your method here
}
Related
I have an EditText in my app with an input type of number:
<EditText
android:id="#+id/answerText"
style="#style/GeneralTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/question"
android:layout_toRightOf="#id/equals"
android:inputType="number"
android:paddingTop="#dimen/activity_vertical_margin"
android:maxLength="3"
android:visibility="invisible"
tools:text="ans"/>
So when the EditText has focus the numeric keyboard is displayed. I am trying to repond to the user pressing the tick button after supplying a value in the EditText but I can't get it to work.
I've tried the following code which I've put inside the onCreate method but it isn't working:
answerGivenText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE){
return true;
}
return false;
}
});
Can anyone help please?
I think you should add the
android:imeOptions="actionDone"
attribute to your edittext.
That code is exactly what you need, but you are not doing anything inside it, you're just intercepting the click and returning true, indicating that you handled it to other listeners on the stack.
You would do whatever it is you want to do when the user clicks the tick in the
if (actionId == EditorInfo.IME_ACTION_DONE)
section. And turn on the DONE action in your layout. What exactly are you trying to achieve?
I am using the OnKeyListener on a EditText on Android to identify when the backspace(delete) of the keyboard was pressed and handle myself the deletion of the character(doing this because it's a field that were synchronized with external service).
Some people said to use the TextWatcher and handle in one of the textchanged events. While this can be a viable solution. I still trying to do it with the onKey.
The problem is that the onKey is only triggered when there is not text(empty) in the EditText and pressed backspace. If there's text the onKey is not triggered until it deletes all text.
Anyone What I am missing for the onKey to be triggered in all the clicks in the backspace?
This is my xml:
<EditText
android:id="#+id/trackpad_speller_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_margin="10dp"
android:layout_weight="0.8"
android:focusable="true"
android:focusableInTouchMode="true"
android:imeOptions="actionDone" />
and I am setting the listener on the fragment
this.spellerInput.setOnKeyListener(this);
And my OnKey
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
if (presenter.getSpellerType() == SpellerType.FREE_SPELLER) {
Log.d(TAG, "backspace pressed and ignored for FREE_SPELLER");
return false;
} else {
Log.d(TAG, "backspace pressed for SEARCH_SPELLER");
presenter.deleteChar();
}
}
return true;
}
Any help would be appreciated! Thanks :D
I don't know if this is the best way, but I found that setting the setInputType of the EditText InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD solves the problem and makes the onKey be triggered in all backspace clicks.
spellerInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
Hope it helps
Hello i have one key listener
email = (EditText)findViewById(R.id.email);
email.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
return true;
}else{
return false;
}
}
});
but when I press enter the event does not work, any idea?
Check the documentation for the OnKeyListener.
Interface definition for a callback to be invoked when a hardware key
event is dispatched to this view. The callback will be invoked before
the key event is given to the view. This is only useful for hardware
keyboards; a software input method has no obligation to trigger this
listener.
I'm guessing your are not using a hardware keyboard.
Yes i have one solution, im add a android:singleLine="true", regards!
<EditText
android:layout_width="fill_parent"
android:layout_height="52dp"
android:id="#+id/email"
android:background="#drawable/customs_borders"
android:textColor="#ffd3e8d6"
android:gravity="center"
android:textStyle="bold"
android:textSize="23dp"
android:hint="EMAIL"
android:singleLine="true"/>
I have 2 EditText with id (et_1 and et_2) in a layout, and I set
for et_1
android:imeOptions="actionNext",
android:nextFocusForward="#+id/et_2"
when et_1 gets focus and the the software keyboard shows "Next", and when I click "Next", the focus doesn't go to et_2, but to another EditText in another fragment layout. Why is it this way?
How to make the focus go to et_2?
Thanks.
Try changing,
android:nextFocusForward="#+id/et_2"
to
android:nextFocusDown="#+id/et_2"
I hope it helps!
I had the same problem in one of my apps. What I did instead was have a listener in my activity for when "Next" was pressed and then just manually changed the focus. Like this:
final EditText et_1 = (EditText)findViewById(R.id.et_1);
final EditText et_2 = (EditText)findViewById(R.id.et_2);
// listen on the et_1 EditText
et_1.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_NEXT) { // "Next was selected"
et_2.requestFocus(); // focus on et_2
}
return true;
}
});
I realised referencing does not hide the keyboard
android:nextFocusDown="#+id/et_2"
To Hide the Keyboard add actionDone
<EditText
android:id="#+id/editTextEnterAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Amount"
android:imeOptions="actionDone"
/>
I have an EditText, its taking inputs of letters and different symbols however not numbers. Additionally, I have set the EditText on setOnKeyListener to close the virtual keyboard by pressing Enter. Observed and found out that it failing to take numerical inputs due to the below code
Code for setting virtual keyboard to hide after pressing enter
durOnTreadmill.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(durOnTreadmill.getWindowToken(), 0);
}
return true;
}
});
my EditText in XML
<EditText
android:id="#+id/durOnTreadmill"
android:layout_width="129dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="32dp"
android:inputType="text"
android:hint="hh:mm:ss" />
Where am I going wrong?
Copying my comment here:
You should only return true from onKey if you're handling the input.
I.e., move the return true inside your if block and return false
otherwise.
android:imeOptions="actionDone"
in you EditText xml will dissmiss the keyboard when the user hits enter
and you say that it wont except numbers you have the input set to "text"
take this line out:
android:inputType="text"