ANDROID:How to open web page in class extends CCLayer

时光总嘲笑我的痴心妄想 提交于 2019-12-20 06:36:56

问题


I am newbie in cocos2d game development and developed 50% game using of Cocos2d SDK Android.I am stuck when the user plays certain level of game more than 3 times then i have to redirect him to google play url to rate my app but not able to do this as i am not able to open the webpage as we did in a class extends activity.I searched every where but there is no such help found to help my cause.

Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse("url"));
                startActivity(in);

My class extends CCLayer is:

public class GameLayer extends CCLayer {
     public int count=0;
    public enum GAME_STATE {
        GS_PLAY,
        GS_PAUSE,
        GS_GAMEOVER
        };

    public CCSprite background;
    public CCSprite road;

    public ArrayList<CCSprite> backArray;
    public ArrayList<CCSprite> frontArray;

    public ArrayList<CCSprite> sunArray;
    public ArrayList<CCSprite> bloodArray;

    public CCSprite die;
    public CCSprite hit;
    public CCSprite smoke;
    public CCSprite blood;

    public CCLabel lbScore;
    public CCMenuItemToggle btPause;
    public CCMenuItemImage btMainMenu;
    public CCMenuItemImage btRetry;

    public Player player;

    public float score;
    public CCSprite scores;
    public CCLabel lbDistance;
  //  public CCLabel lbnotifi;
    public CCLabel lbCurrentScore;
    public CCLabel lbPersonalBest;
    public CCLabel lbBestScore;

    public  GAME_STATE state;   

    public float POS_X_PREV_SOUND  =  (G.rwidth / 3);
    public float POS_Y_PREV_SOUND  =  (G.rheight * 5 / 7);

    public float POS_X_NEXT_SOUND  =  (G.rwidth / 3 * 2);
    public float POS_Y_NEXT_SMOKE  = (G.rheight * 4 / 7);

    public CGPoint curDiePos;


    public GameLayer()
    {
        backArray = new ArrayList<CCSprite>();
        frontArray = new ArrayList<CCSprite>();
        sunArray = new ArrayList<CCSprite>();
        bloodArray = new ArrayList<CCSprite>();

        this.setIsTouchEnabled(true);

        background = CCSprite.sprite("background/back_ipad.png", CGRect.make(0, 0, 1024, 768));
        background.setPosition(G.rwidth / 2, G.rheight / 2);
        background.setScaleX(G.rX);
        background.setScaleY(G.rY);
        addChild(background);

        CCSprite sp = CCSprite.sprite("background/back_ipad.png", CGRect.make(0, 846, 1024, 178));
        sp.setPosition(G.rwidth / 2, sp.getContentSize().height / 2 * G.rY);
        sp.setScaleX(G.rX);
        sp.setScaleY(G.rY);
        addChild(sp);
        backArray.add(sp);

        sp = CCSprite.sprite("background/back_ipad.png", CGRect.make(0, 846, 1024, 178));
        sp.setPosition(G.rwidth * 3 / 2, sp.getContentSize().height / 2 * G.rY);
        sp.setScaleX(G.rX);
        sp.setScaleY(G.rY);
        addChild(sp);
        backArray.add(sp); 

        sp = CCSprite.sprite("background/front_mountion_ipad.png");
        sp.setPosition(G.rwidth / 2, sp.getContentSize().height / 2 * G.rY);
        sp.setScaleX(G.rX);
        sp.setScaleY(G.rY);
        addChild(sp);
        frontArray.add(sp);

        sp = CCSprite.sprite("background/front_mountion_ipad.png");
        sp.setPosition(G.rwidth * 3 / 2, sp.getContentSize().height / 2 * G.rY);
        sp.setScaleX(G.rX);
        sp.setScaleY(G.rY);
        addChild(sp);
        frontArray.add(sp);

        road = CCSprite.sprite("background/back_ipad.png", CGRect.make(0, 768, 1024, 24));
        road.setAnchorPoint(0.5f, 1);
        road.setPosition(G.rwidth / 2, G.LOAD_HEIGHT);
        road.setScaleX(G.rX);
        road.setScaleY(G.rY);
        addChild(road);

        lbScore = CCLabel.makeLabel("0", "arial", 30);
        lbScore.setScale(G.rX);
        lbScore.setPosition(G.rwidth - 70 * G.rX, G.rheight - 70 * G.rY);
        lbScore.setColor(new ccColor3B(0, 0, 0));
        addChild(lbScore);

        btPause = CCMenuItemToggle.item(this, "selPause",
                                        CCMenuItemImage.item("object/bt_pause.png", "object/bt_pause.png"),
                                        CCMenuItemImage.item("object/bt_right01.png", "object/bt_right02.png"));
        btPause.setScale(G.rX);
        btPause.setPosition(70 * G.rX, G.rheight - 70 * G.rY);

        btMainMenu = CCMenuItemImage.item("object/bt_main01.png", "object/bt_main02.png", this, "selMainMenu");
        btMainMenu.setScale(G.rX);
        btMainMenu.setPosition(G.rwidth / 4, G.rheight / 4);

        btRetry = CCMenuItemImage.item("object/bt_retry01.png", "object/bt_retry02.png", this, "selRetry");
        btRetry.setScale(G.rX);
        btRetry.setPosition(G.rwidth * 3 / 4, G.rheight / 4);

        CCMenu menu = CCMenu.menu(btPause, btMainMenu, btRetry);
        menu.setPosition(0, 0);
        addChild(menu, 5);

        player = new Player();
        player.setPosition(G.PLAYER_POS, G.LOAD_HEIGHT);
        addChild(player, 5);

        smoke = CCSprite.sprite("smoke/smoke_000.png");
        smoke.setAnchorPoint(0.5f, 0);
        smoke.setScale(G.rX);
        smoke.setPosition(player.getPosition());
        addChild(smoke);
        CCTextureCache.sharedTextureCache().removeTexture(smoke.getTexture());

        hit = CCSprite.sprite("object/c0001.png");
        hit.setAnchorPoint(0.5f, 0.5f);
        hit.setScale(G.rX);
        hit.setPosition(CGPoint.ccpAdd(player.getPosition(),
                CGPoint.ccp(player.spRun.getContentSize().width / 2 * G.rX,
                        player.spRun.getContentSize().height / 2 * G.rX)));
        addChild(hit);
        CCTextureCache.sharedTextureCache().removeTexture(hit.getTexture());

        blood = CCSprite.sprite("object/blood_000.png");
        blood.setScale(G.rX);
        blood.setPosition(0, 0);
        addChild(blood);
        CCTextureCache.sharedTextureCache().removeTexture(blood.getTexture());

        die = CCSprite.sprite("object/tombstone00.png");
        die.setAnchorPoint(0.5f, 0);
        die.setScale(G.rX);
        die.setPosition(player.getPosition());     
        addChild(die);
        CCTextureCache.sharedTextureCache().removeTexture(die.getTexture());

        scores = CCSprite.sprite("object/t_score.png");
        scores.setScale(G.rX);
        scores.setPosition(G.rwidth / 2, 670 * G.rY);
        addChild(scores);

        lbDistance = CCLabel.makeLabel("Distance:", CGSize.make(G.rwidth, 150*G.rY), TextAlignment.LEFT, "arial", 35);
        lbDistance.setAnchorPoint(0, 0.5f);
        lbDistance.setScale(G.rX);
        lbDistance.setPosition(POS_X_PREV_SOUND, POS_Y_PREV_SOUND);
        ccColor3B col = new ccColor3B(0, 90, 0);
        lbDistance.setColor(col);
        addChild(lbDistance);

//        
//        lbnotifi = CCLabel.makeLabel("Distance:", CGSize.make(G.rwidth, 150*G.rY), TextAlignment.LEFT, "arial", 35);
//        lbnotifi.setAnchorPoint(0, 0.5f);
//        lbnotifi.setScale(G.rX);
//        lbnotifi.setPosition(POS_X_PREV_SOUND, POS_Y_PREV_SOUND);
//        ccColor3B col1 = new ccColor3B(0, 90, 0);
//        lbDistance.setColor(col1);
//      //  addChild(lbDistance);


        lbCurrentScore = CCLabel.makeLabel("0", CGSize.make(200*G.rX, 150*G.rY), TextAlignment.RIGHT, "arial", 35);
        lbCurrentScore.setScale(G.rX);
        lbCurrentScore.setPosition(POS_X_NEXT_SOUND, POS_Y_PREV_SOUND);
        lbCurrentScore.setColor(col);
        addChild(lbCurrentScore);

        lbPersonalBest = CCLabel.makeLabel("Your Best Run:", CGSize.make(G.rwidth, 150*G.rY), TextAlignment.LEFT, "arial", 35);
        lbPersonalBest.setAnchorPoint(0, 0.5f);
        lbPersonalBest.setScale(G.rX);
        lbPersonalBest.setPosition(POS_X_PREV_SOUND, POS_Y_NEXT_SMOKE);
        lbPersonalBest.setColor(col);
        addChild(lbPersonalBest);

        lbBestScore = CCLabel.makeLabel("0", CGSize.make(200*G.rX, 150*G.rY), TextAlignment.RIGHT, "arial", 35);
        lbBestScore.setScale(G.rX);
        lbBestScore.setPosition(POS_X_NEXT_SOUND, POS_Y_NEXT_SMOKE);
        lbBestScore.setColor(col);
        addChild(lbBestScore);

        startGame();
    }

    public CGRect sunRect(CCSprite  sp)
    {
        CGRect rt;
        CGSize sz = sp.getContentSize();
        sz.width = sz.width * G.rX;
        sz.height = sz.height * G.rY;

        rt = CGRect.make(sp.getPosition().x, sp.getPosition().y, sz.width, sz.height);

        return rt;
    }

    public void  loadBloods()
    {
        if (bloodArray != null) {
            int arrayCount = bloodArray.size();

            if (arrayCount > 0) {
                for (int i = 0; i < arrayCount; i++)
                {
                    CCSprite spBlood = bloodArray.get(i); 
                    removeSprite(spBlood);
                }
            }
            bloodArray.clear();
        }

        CCSprite sp;

        for (int i = 0; G.DATA_BLOOD[i][0] != 0; i ++) {
            int imageNum = (int) G.DATA_BLOOD[i][1];
            sp = CCSprite.sprite(G.IMG_BLOOD[ imageNum ]);
            sp.setScale(G.rX);
            sp.setAnchorPoint(0, 1);
            sp.setPosition(G.DATA_BLOOD[i][0] * G.rX, G.LOAD_HEIGHT);
            addChild(sp);
            bloodArray.add(sp);
        }
    }

    public void  loadSuns()
    {
        if (sunArray != null) {
            int arrayCount = sunArray.size();

            if (arrayCount > 0) {
                for (int i = 0; i < arrayCount; i++)
                {
                    CCSprite sun = sunArray.get(i);
                    removeSprite(sun);
                }
            }
            sunArray.clear();

        }

        CCSprite sp;


    }

    public void  updateScore()
    {
        lbScore.setString(String.format("%d", (int)score));
    }

    public void  initPlayer()
    {
        player.state = PLAYER_STATE.PS_RUN;
        player.spRun.setVisible(true);
        player.spJump.setVisible(false);
        player.spDown.setVisible(false);
        player.runBlood.setVisible(false);

        player.timeStateUpdate(PLAYER_STATE.PS_RUN);
        player.startRunAnimation();
        player.setVisible(true);
    }

    public void  startGame()
    {
        state = GAME_STATE.GS_PLAY;
        score = 0;
        updateScore();      

        btMainMenu.setVisible(false);
        btRetry.setVisible(false);
        scores.setVisible(false);
        lbDistance.setVisible(false);
       // lbnotifi.setVisible(false);
        lbCurrentScore.setVisible(false);
        lbPersonalBest.setVisible(false);
        lbBestScore.setVisible(false);
        lbScore.setVisible(true);
        btPause.setVisible(true);
        btPause.setSelectedIndex(0);

        die.setVisible(false);

        CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage("object/tombstone00.png");
        if( texture != null)
        {
            die.setTexture(texture);
        }
        CCTextureCache.sharedTextureCache().removeTexture(die.getTexture());


        hit.setVisible(false);
        hit.setOpacity(255);
        texture = CCTextureCache.sharedTextureCache().addImage("object/c0001.png");
        if( texture != null ) 
        {
            hit.setTexture(texture);
        }
        CCTextureCache.sharedTextureCache().removeTexture(hit.getTexture());

        blood.setVisible(false);
        texture = CCTextureCache.sharedTextureCache().addImage("object/blood_000.png");
        if( texture != null)
        {
            blood.setTexture(texture);
        }
        CCTextureCache.sharedTextureCache().removeTexture(blood.getTexture());

        smoke.stopAllActions();
        smoke.setVisible(false);
        texture = CCTextureCache.sharedTextureCache().addImage("smoke/smoke_000.png");
        if( texture != null) 
        {
            smoke.setTexture(texture);
        }
        CCTextureCache.sharedTextureCache().removeTexture(smoke.getTexture());


        loadSuns();
        if (G.g_gameInfo.isSmokeOn == true)
        {

            loadBloods();
        }
        initPlayer();
        this.schedule("onTime", 0.02f);

    }

    public void  selMainMenu()
    {
        G.sd_button.start();
        CCScene scene = CCScene.node();
        scene.addChild(new FirstScene(), 1);
        CCDirector.sharedDirector().replaceScene(CCFadeTransition.transition(1.0f, scene));
    }

    **public void  selRetry()
    {
        count++;
        G.sd_button.start();
        if(count==3)
        {

        }
        if(count<3){
        startGame();
        }
    }**

    public void  processMovingObjects(float dt)
    {
        float backDelta = G.VEL_BACK_MOVE * dt;
        float frontDelta = G.VEL_FRONT_MOVE * dt;
        float sunDelta = G.VEL_RUN * dt;

        score += backDelta / 2;
        updateScore();

        for (int i = 0; i < backArray.size(); i++)
        {
            CCSprite sp = backArray.get(i);
            if ( (sp.getPosition().x + sp.getContentSize().width / 2 * G.rX) < 0)
            {
                sp.setPosition(CGPoint.ccpAdd(sp.getPosition(), CGPoint.ccp(G.rwidth * 2, 0)));
            }

            sp.setPosition(CGPoint.ccpSub(sp.getPosition(), CGPoint.ccp(backDelta, 0)));
        }

        for (int i = 0; i < frontArray.size(); i++) 
        {
            CCSprite sp = frontArray.get(i);
            if ( (sp.getPosition().x + sp.getContentSize().width / 2 * G.rX) < 0)
            {
                sp.setPosition(CGPoint.ccpAdd(sp.getPosition(), CGPoint.ccp(G.rwidth * 2, 0)));
            }

            sp.setPosition(CGPoint.ccpSub(sp.getPosition(), CGPoint.ccp(frontDelta, 0)));
        }

        for (int i = 0; i < sunArray.size(); i++) 
        {
            CCSprite sp = sunArray.get(i);
            sp.setPosition(CGPoint.ccpSub(sp.getPosition(), CGPoint.ccp(sunDelta, 0)));

            if ( (sp.getPosition().x + sp.getContentSize().width / 2 * G.rX) < 0) 
            {
                sp.setPosition(CGPoint.ccpAdd(sp.getPosition(), CGPoint.ccp(G.rwidth * 5, 0)));
            }
        }

        if (G.g_gameInfo.isSmokeOn == true)
        {

            for (int i = 0; i < bloodArray.size(); i++)
            {
                CCSprite sp = bloodArray.get(i);
                sp.setPosition(CGPoint.ccpSub(sp.getPosition(), CGPoint.ccp(sunDelta, 0)));

                if ( (sp.getPosition().x + sp.getContentSize().width / 2 * G.rX) < 0)
                {
                    sp.setPosition(CGPoint.ccpAdd(sp.getPosition(), CGPoint.ccp(G.rwidth * 5, 0)));
                }
            }

        }
    }

    public void  selShowGmaeOverMenu()
    {
        btMainMenu.setVisible(true);
        btRetry.setVisible(true);
        scores.setVisible(true);
        lbDistance.setVisible(true);
        lbCurrentScore.setVisible(true);
        lbPersonalBest.setVisible(true);
        lbBestScore.setVisible(true);

        int bestScore = 0;
        switch (G.g_gameInfo.trackNum) 
        {
            case 0:
                bestScore = G.g_gameInfo.scoreTrack1;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack1 = bestScore;
                break;
            case 1:
                bestScore = G.g_gameInfo.scoreTrack2;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack2 = bestScore;

                break;
            case 2:
                bestScore = G.g_gameInfo.scoreTrack3;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack3 = bestScore;

                break;
            case 3:
                bestScore = G.g_gameInfo.scoreTrack4;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack4 = bestScore;

                break;
            case 4:
                bestScore = G.g_gameInfo.scoreTrack5;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack5 = bestScore;

                break;
            case 5:
                bestScore = G.g_gameInfo.scoreTrack6;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack6 = bestScore;

                break;
            case 6:
                bestScore = G.g_gameInfo.scoreTrack7;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack7 = bestScore;

                break;
            case 7:
                bestScore = G.g_gameInfo.scoreTrack8;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack8 = bestScore;
            case 8:
                bestScore = G.g_gameInfo.scoreTrack9;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack9 = bestScore;
            case 9:
                bestScore = G.g_gameInfo.scoreTrack10;
                if (bestScore < (int)score) {
                    bestScore = (int)score;
                }
                G.g_gameInfo.scoreTrack10 = bestScore;

                break;

            default:
                break;
        }

        G.saveHistory();

        lbCurrentScore.setString(String.format("%d", (int)score));
        lbBestScore.setString(String.format("%d", bestScore));
    }

    public void  selShowDieAni()
    {
        G.sd_fire.start();

        die.runAction(G.ani_die);
        die.setVisible(true);
        die.setPosition(curDiePos.x, G.LOAD_HEIGHT);

    }

    public void  selHideBlood()
    {
        blood.setVisible(false);
    }

    public void  selEndSmoke()
    {
        smoke.setVisible(false);
    }

    public void  gameOver()
    {
        G.sd_delay.start();

        state = GAME_STATE.GS_GAMEOVER;

        btPause.setVisible(false);
        lbScore.setVisible(false);

        for (int i = 0; i < sunArray.size(); i++)
        {
            CCSprite sun = sunArray.get(i);
            sun.stopAllActions();
        }

        this.unschedule("onTime");
        player.stopRunAnimation();
        player.setVisible(false);

//      CCSequence seqSmoke = CCSequence.actions(G.ani_smoke copy] autorelease],
//                              [CCCallFunc actionWithTarget:self selector:@selector(selEndSmoke)], nil];
        smoke.runAction(G.ani_smoke);
        selEndSmoke();

        smoke.setVisible(true);
        smoke.setPosition(curDiePos);

        if (G.g_gameInfo.isSmokeOn == true)
        {
            blood.setVisible(true);
            blood.setPosition(curDiePos.x, G.LOAD_HEIGHT + 23 * G.rX);

//          CCSequence *seqBlood = [CCSequence actions:[[ani_blood copy] autorelease],
//                                  [CCCallFunc actionWithTarget:self selector:@selector(selHideBlood)], nil];

            blood.runAction(G.ani_blood);
            selHideBlood();

            CCSequence seq = CCSequence.actions(CCCallFunc.action(this, "selShowDieAni"),
                               CCCallFunc.action(this, "selShowGmaeOverMenu"));

            this.runAction(seq);
        }
        else {

            CCSequence seq = CCSequence.actions(CCFadeOut.action(0.3f),
                               CCCallFunc.action(this, "selShowDieAni"),
                               CCCallFunc.action(this, "selShowGmaeOverMenu"));

            hit.runAction(G.ani_hit);
            hit.runAction(seq);
            hit.setVisible(true);
            hit.setPosition(curDiePos.x, G.LOAD_HEIGHT + 23 * G.rX);

        }



    }

    public void  scanCollision()
    {
        CGRect rcPlayer = player.playerRect();

        for (int i = 0; i < sunArray.size(); i++) 
        {
            CCSprite sun = sunArray.get(i);
            CGRect rcSun = this.sunRect(sun);

            if (CGRect.intersects(rcPlayer, rcSun))
            {

                curDiePos = CGPoint.ccpAdd(sun.getPosition(), CGPoint.ccp(sun.getContentSize().width / 2 * G.rX, 0));
                sun.setVisible(false);

                gameOver();
                break;
            }
        }

        if (G.g_gameInfo.isSmokeOn == true)
        {

            for (int i = 0; i < bloodArray.size(); i++) 
            {
                CCSprite spBlood = bloodArray.get(i);
                CGRect rcBlood = this.sunRect(spBlood);

                if (player.getPosition().x > rcBlood.origin.x && player.getPosition().x < rcBlood.origin.x + rcBlood.size.width)
                {
                    player.actionRunBlood();
                }
            }

        }
    }

    public void  onTime(float dt)
    {
        if(dt > 0.02f)
            dt = 0.02f;

        processMovingObjects(dt);
        scanCollision();
    }

    public void  selPause()
    {
        G.sd_button.start();

        if (btPause.selectedIndex() == 0)
        {
            CCDirector.sharedDirector().resume();
            state = GAME_STATE.GS_PLAY;
        }
        else
        {

            CCDirector.sharedDirector().pause();
            state = GAME_STATE.GS_PAUSE;
        }
    }

    public boolean  ccTouchesBegan(MotionEvent event)
    {
        CGPoint touchPoint = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));

        if (state == GAME_STATE.GS_PLAY)
        {
            if (touchPoint.x < G.rwidth / 2) {
                player.timeStateUpdate(PLAYER_STATE.PS_DOWN);
            }
            else {        
                player.timeStateUpdate(PLAYER_STATE.PS_JUMP);

            }

        }

        return true;
    }

    public boolean  ccTouchesEnded(MotionEvent event)
    {
        CGPoint touchPoint = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));

        if (state == GAME_STATE.GS_PLAY)
        {
            if (touchPoint.x < G.rwidth / 2) {
                if (player.state == PLAYER_STATE.PS_DOWN)
                {
                    player.timeStateUpdate(PLAYER_STATE.PS_RUN);
                }
            }
            else {
                if (player.state == PLAYER_STATE.PS_JUMP)
                {
                    player.timeStateUpdate(PLAYER_STATE.PS_RUN);
                }
            }

        }

        return true;
    }

    public boolean  ccTouchesCancelled(MotionEvent event)
    {
        CGPoint touchPoint = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));

        if (state == GAME_STATE.GS_PLAY) 
        {
            if (touchPoint.x < G.rwidth / 2)
            {
                if (player.state == PLAYER_STATE.PS_DOWN)
                {
                    player.timeStateUpdate(PLAYER_STATE.PS_RUN);
                }
            }
            else {
                if (player.state == PLAYER_STATE.PS_JUMP)
                {
                    player.timeStateUpdate(PLAYER_STATE.PS_RUN);
                }
            }

        }

        return true;
    }
        public void onExit()
    {
        removeCache();
        super.onExit();

    }

}

Please assist me.Thanks in Advance


回答1:


Okay, the fundamental thing you have to remember with Cocos2d is that your ccScenes/Layers etc are all running INSIDE an activity.

what you need is to create and expose a Handler on your activity, and post a Runnable to that handler from your CCLayer.

You could then inflate a layout containing a WebView and a 'close' button in that runnable and attach them to the content view.

from there it's just a matter of attaching an onClick to the close button to set that layout to View.GONE and remove the objects, and you should be well away.

hope this helps

I've had to do this on my app, from the main menu layer, to show the web based tutorial page, and it works a treat.

} else if (i==TUTORIAL) {
//call load web url here
    Handler handler = HomeMenu.getHandler();
    handler.post(new Runnable() {
        public void run() {
            HomeMenu activity = HomeMenu.getInstance();

            LayoutInflater inflater = (LayoutInflater)activity.getLayoutInflater();
            if(inflater != null)
            {
                baseLayout = inflater.inflate(R.layout.tutoriallayout, null);
            } else {
                throw new IllegalStateException("Unable to find view inflater");
            }
            WebView wv = (WebView) baseLayout.findViewById(R.id.tutorialWeb);
            wv.loadUrl( "http://url to your tutorial here" );
            ImageView iv = (ImageView) baseLayout.findViewById(R.id.closeTutorial);
            iv.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    if (baseLayout != null) {
                        baseLayout.setVisibility(View.GONE);
                    }
                    baseLayout = null;
                }
            });

            activity.addContentView(baseLayout,  new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        }
    });
    break;
}

and a layout such as

<?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/absoluteLayout1" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content">
        <RelativeLayout android:id="@+id/rLayout1" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content"
            android:padding="10dip">
            <android.webkit.WebView android:id="@+id/tutorialWeb" android:layout_height="fill_parent" android:layout_width="fill_parent" android:padding="10dip"></android.webkit.WebView>
        </RelativeLayout>
        <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/delete" android:id="@+id/closeTutorial" android:adjustViewBounds="true" android:clickable="true"></ImageView>
    </AbsoluteLayout>    


来源:https://stackoverflow.com/questions/10281446/androidhow-to-open-web-page-in-class-extends-cclayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!