I have a simple code that shows strings in a TextView after a button is pressed or when the user press ‘Enter’. When the user presses the button all is find, but when the ‘enter’ is pressed, it calls performClick() to call the same function than the button does. But my fonction is always called twice :
private OnKeyListener ChampKeyListener = new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_ENTER)
{
recherche.performClick(); // recherche is my button
}
return false;
}
};
private OnClickListener RechercheListener = new OnClickListener() {
@Override
public void onClick(View v) {
//whatever I have tried here it is always called twice
}
};
How can I stop that. I have seen that I could solve that my going to another View or Activity but I don’t want to use those.
Any hint? Thanks!
As there are two actions on press
KeyEvent.ACTION_DOWN&KeyEvent.ACTION_UPhttp://developer.android.com/reference/android/view/View.OnKeyListener.html
Returns
trueif the listener has consumed the event,falseotherwise.Try it…