0

My problem is similar to question posted here. I want an Android button to stay pressed. I cannot follow the solution provided because onClick will be called when a button is pressed via keyboard or trackball and i need to handle that.

I tried setting button.setPressed(true); in onClick callback , but it doesn't seem to work. Is there a way to do this?

4

2 回答 2

2

Try this it will work...

singIn.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction()==MotionEvent.ACTION_DOWN) return true;
                    if(event.getAction()!=MotionEvent.ACTION_UP) return false;

                  //DO SOMETHING!!

                    singIn.setPressed(true);                    
                    return true;
                }


            });
于 2012-09-06T11:24:55.303 回答
1

Try sending a touch event to the button like this:

MotionEvent down = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0);
yourButton.dispatchTouchEvent(down);
于 2012-09-05T05:30:37.023 回答