Reputation: 33
I Have a Button which will do a bunch of Stuff when i type some numbers in an EditText.I want to know how to trigger this Button by simply pressing the "Enter" key. For now i only have an onClickListener which the user has to press once he has entered the data into the EditText.I want the Button to remain there and provide the user an additional option for proceeding further.
Can anyone help? The Code Below does nothing.When I press Enter on the Keyboard it just goes to the next line...
EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)){
Button.performClick();
}
return false;
}
});
Upvotes: 2
Views: 4451
Reputation: 66
This code works for me
youredittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Enter pressed");
}
return false;
}
});
Upvotes: 4
Reputation: 7881
Refer below code for your solution ,
editText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent)
{
if (id == EditorInfo.IME_NULL)
{
//Do your button action here
return true;
}
return false;
}
});
Upvotes: 0
Reputation: 2757
See this:
edittext.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
Toast.makeText(MainActivity.this,edittext.getText(), Toast.LENGTH_LONG).show();
return true;
}
Upvotes: 0