Reputation: 13
I have written a code to implement onclick events of buttons in android. I have written the following line to set the listener in oncreate method. I want to recognize the touch event of a button in the application.i am getting a class cast exception in android.
mybtn[i].setOnClickListener( (OnClick Lisener)this);
for the alternatives to recognize the ontouch event i have looked at the SetKeylistener() , setonkeylistener() and setontouchlistener() methods. but i am not getting which one to use exactly.
What exactly i want to achieve is TTS should speak the number when the button is touched, and on the key release that number should be added to some textbox. so what methods should i use to accomplish these things.
Upvotes: 0
Views: 675
Reputation: 29199
Implement interface in your activity class which ever listener you want to listen on button, like for onClickListener
public class A extends Activity implments OnClickListener
Upvotes: 0
Reputation: 671
use View.OnClickListener if you use setOnClickListener.
To accomplish your purpose you can btn.setOnTouchListener and do something according to the MotionEvent,such as event.getAction=MotionEvent.ACTION_UP ...
for example:
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
return false;
}
});
Upvotes: 0
Reputation: 5414
You should accomplish the effect by doing this:
mybtn[i].setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
// Do some stuff
break;
case MotionEvent.ACTION_UP:
// Do some stuff
break;
}
return false;
}
});
Upvotes: 1