开发者

Android: startActivity() in a class that extends View

开发者 https://www.devze.com 2023-02-03 08:40 出处:网络
I have 3 classes: MainActivity,DrawView and SecondActivity. MainActivity just use DrawView to draw something on the screen. How can I start SecondActivity when i touch the screen on an Bitmap, I know

I have 3 classes: MainActivity,DrawView and SecondActivity. MainActivity just use DrawView to draw something on the screen. How can I start SecondActivity when i touch the screen on an Bitmap, I know where is the bitmap(in the code). The onTouchEvent function from DrawView class.

@Override
public boolean onTouchEvent(final MotionEvent ev) {
     switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {

            float posX = ev.getX();
            float posY = ev.getY();
            if(play.touched(posX, posY)==true){

            } 开发者_高级运维else {
            }
            return true;
        }

}
     return false;
}

The function play.touched(posX,posY) return a boolean value(I made a class that handel the position of the Bitmap). How can I start Second Activity from here?


        if(play.touched(posX, posY)){
            Intent intent = new Intent(getContext(), SecondActivity.class);
            ((Activity)getContext()).startActivity(intent);
        } else {
        }


Have you tried:

if(play.touched(posX, posY)){
    Intent intent = new Intent(getContext(), SecondActivity.class);
    super.getContext().startActivity(intent);
} else {
}
0

精彩评论

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