0

我是 cocos2d 游戏开发的新手,使用 Cocos2d SDK Android 开发了 50% 的游戏。当用户玩一定级别的游戏超过 3 次时我被卡住了,然后我必须将他重定向到 google play url 来评价我的应用程序但不能要做到这一点,因为我无法像在课堂扩展活动中那样打开网页。我到处搜索,但没有找到这样的帮助来帮助我的事业。

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

我的类扩展 CCLayer 是:

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();

    }

}

请帮助我。在此先感谢

4

1 回答 1

0

好的,对于 Cocos2d,您必须记住的基本事项是您的 ccScenes/Layers 等都在活动内部运行。

您需要在您的活动上创建和公开一个处理程序,并从您的 CCLayer 向该处理程序发布一个 Runnable。

然后,您可以在该可运行文件中扩展包含 WebView 和“关闭”按钮的布局,并将它们附加到内容视图。

从那里只需将 onClick 附加到关闭按钮即可将该布局设置为 View.GONE 并删除对象,您应该远离。

希望这可以帮助

我必须在我的应用程序上从主菜单层执行此操作,以显示基于 Web 的教程页面,这很有效。

} 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;
}

和布局,如

<?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>    
于 2012-04-23T16:07:30.727 回答