Issue
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!
Solution
As there are two actions on press
KeyEvent.ACTION_DOWN
&KeyEvent.ACTION_UP
http://developer.android.com/reference/android/view/View.OnKeyListener.html
Returns true
if the listener has consumed the event, false
otherwise.
Try it...
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
if(keyCode == KeyEvent.KEYCODE_ENTER)
{
recherche.performClick(); // recherche is my button
return true;
}
}
return false;
}
Answered By - Dheeresh Singh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.