Now I see. I will suggest a following solution. Suppose we have an
activity. Lets create a handler. Than add to our "channel up" button
View.OnTouchListener. Each time user presses the button, we will
increase channel number and send a message to handler with FIRST_DELAY
delay time. And when user releases button we will tell handler to
dispose all of the queued messages. Handler, on it's part, will
receive a message, increase channel, and send a message to itself with
delay CONT_DELAY. Generally this two delays can be the same, but I
think user will be more comfortable if automatic channel switching
starts in a while, not immediatly. So this piece of mind above can be
expressed in the following code:
public class ChannelSwitcherActivity extends Activity
{
private static final long FIRST_DELAY = 500;
private static final long CONT_DELAY = 200;
private static final int MESSAGE_INCREASE_CHANNEL = 1;
private Handler switchHandler = new Handler()
{
@Override
public void handleMessage(Message message)
{
if( MESSAGE_INCREASE_CHANNEL == message.what )
{
channelUp();
sendEmptyMessageDelayed(
MESSAGE_INCREASE_CHANNEL, CONT_DELAY);
}
}
};
@Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
final Button btn = (Button)findViewById( R.id.channel_up_btn);
btn.setOnTouchListener( new View.OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
if( view instanceof Button )
{
if( MotionEvent.ACTION_DOWN ==
event.getAction() )
{
channelUp();
switchHandler.sendEmptyMessageDelayed( MESSAGE_INCREASE_CHANNEL,
FIRST_DELAY);
return true;
}
else if( MotionEvent.ACTION_UP ==
event.getAction() )
{
switchHandler.removeMessages(
MESSAGE_INCREASE_CHANNEL);
return true;
}
}
return false;
}
});
}
private void channelUp()
{
//this is where you place your logic to increase channel by one
position
}
}
Hope this helps.
~~~~~~~~~~~~~
Regards,
Alexander Kazanin
http://sites.google.com/site/piggybanksoftwarehomepage
On Oct 4, 7:49 am, Shude Zheng <[email protected]> wrote:
> Hi, Alexander,
>
> Thank you. I have not describe the question clearly.
>
> I want change the channel by pressing button. But press button once and the
> callback is called once. The channel only one channel up. But I want to
> change more channels. So I press the button and hold it. The callback will
> call a function change_channel(1). This function will increase the channel
> for every 0.2s. When user release the button the button state is changed
> again so the callback will kill the change_channel(0) and stop the channel
> change.
>
> I found OnFocusChangeListener did not work for pressing and hold of button.
> It only works when cursor moving by direction key.
>
> Shude
>
> On Fri, Oct 2, 2009 at 4:25 AM, Alexander Kazanin <
>
> [email protected]> wrote:
>
> > Hi.
>
> > If I understood your intentions correctly you should set
> > OnLongClickListener.
> > Button button = (Button) findViewById(R.id.*channel_up*);
>
> > button.setOnFocusChangeListener(new View.OnLongClickListener()
> > {
> > public boolean onLongClick(View view)
> > {
> > return false;
> > }
> > });
>
> > method onLongClick in this anonymous class will be called when user
> > presses and holds "cannnel_up" button for about 2 seconds
>
> > On 2 окт, 03:08, Shude Zheng <[email protected]> wrote:
> > > I want to add some code when user press and hold the button. I try use
> > > change the focus function. I wrote the similar code like onClick. But it
> > was
> > > never called when I moved curer to button and it was focused.
>
> > > Button button = (Button) findViewById(R.id.*channel_up*);
>
> > > button.setOnFocusChangeListener(*new* Button.OnFocusChangeListener() {
>
> > > *public* *void* onFocusChange(View v, *boolean* hasFocus) {
>
> > > }
>
> > > Thanks
>
> > > Shude
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---