开发者

Android ontouchlistener help

I want to know that if I have two activities in a program then how can I switch between two activities using ontouchlistener, just only touching anywhere on the screen?

public class V开发者_运维技巧19 extends Activity implements OnTouchListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay19);
    }

    @Override
    public boolean onTouch(View to_main, MotionEvent event) {
        return false;
    }
}


You can use layout inflater and method setContentView(View v)

public class V19 extends Activity implements OnTouchListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = getLayoutInflater().inflate(R.layout.lay19, null);
    setContentView(view);
    view.setOnTouchListener(this);
}

@Override
public boolean onTouch(View to_main, MotionEvent event) {
    Intent i = new Intent(this, Activity2.class);
    startActivity(i);
    return false;
}
}    

And than you can catch all touch events.

Other way is to override public boolean dispatchTouchEvent (MotionEvent ev) method. Reference says:

public boolean dispatchTouchEvent (MotionEvent ev)

Since: API Level 1 Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally. Parameters

ev The touch screen event.

Return true if this event was consumed.


You will need to pin a listener to different aspects in your layout (buttons, etc.)

To have the device pick up touch events anywhere, I've used public boolean onTouchEvent(final MotionEvent event), which will hold the entire event performed (how many fingers, position, etc.)


by following this tutorial you should be able to move from one to the other.

The tutorials uses a button click but it should be possible to replace that with what you want.

Basically you need to have your activity made(which I assume you do) Call your intent and activity onTouch instead of on a button click.

And don't forget to declare your new activity in the manifest.

        <activity android:name=".YourActivityName"></activity>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜