2012-04-17 42 views
0

你好,我使用Android構建一個小應用程序,在這個應用程序中,我建立了兩個活動,現在當我點擊第一個活動的按鈕(新建按鈕的名稱)活動,它什麼都不做,但是當我再次點擊這個按鈕時,它會改變活動。我想在一個點擊這裏我發佈我的兩個活動的代碼,請幫助我。 由於在第一個活動Android活動轉移不會發生在單一時間

public class TTTActivity extends Activity implements OnClickListener { 
public int result; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    View newButton = findViewById(R.id.new_button); 
    newButton.setOnClickListener(this);   

    View exitButton = findViewById(R.id.exit_button); 
    exitButton.setOnClickListener(this); 

} 
private void quitApplication() 
{ 
    finish(); 
} 
private void startGame(){ 
    startActivity(new Intent(this, Game.class)); 
} 


@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.new_button: 
     //startNewGameConfirm(); 
     startGame(); 
     break; 
    case R.id.exit_button: 
     quitApplication(); 
     break; 

    } 

} 

}遊戲活動的

源代碼的提前 源代碼

public class Game extends Activity { 
private final int GAME_VICTORY = 0x1; 
private final int GAME_DEFEAT = 0x2; 
private final int GAME_TIE = 0x3; 
private final int GAME_CONTINUES = 0x4; 
private final float UNIQUE_MAX_WEIGHT=0.85f; 
static final int ACTIVITY_SELECTION = 1; 

private float[] w; 
private int[] c;   
private int[][] PosTable; 
private Button[] buttons; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.game); 

    buttons = new Button[9]; 

    buttons[0] = (Button) findViewById(R.id.Button01); 
    buttons[0].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(0); 
     } 
    }); 
    buttons[1] = (Button) findViewById(R.id.Button02); 
    buttons[1].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(1); 
     } 
    }); 
    buttons[2] = (Button) findViewById(R.id.Button03); 
    buttons[2].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(2); 
     } 
    }); 
    buttons[3] = (Button) findViewById(R.id.Button04); 
    buttons[3].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(3); 
     } 
    }); 
    buttons[4] = (Button) findViewById(R.id.Button05); 
    buttons[4].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(4); 
     } 
    }); 
    buttons[5] = (Button) findViewById(R.id.Button06); 
    buttons[5].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(5); 
     } 

    }); 
    buttons[6] = (Button) findViewById(R.id.Button07); 
    buttons[6].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(6); 
     } 
    }); 
    buttons[7] = (Button) findViewById(R.id.Button08); 
    buttons[7].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(7); 
     } 
    }); 
    buttons[8] = (Button) findViewById(R.id.Button09); 
    buttons[8].setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      btnClicked(8); 
     } 
    }); 

    Button button = (Button) findViewById(R.id.Button10); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      buttonsEnable(true); 
      beginPlay(); 
     } 
    }); 

    startActivityForResult(new Intent(Game.this, TTTActivity.class), ACTIVITY_SELECTION); 


    DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics(); 
    float h = (float) (dm.heightPixels - (100.0)*dm.density); 
    float w = dm.widthPixels; 
    for(int i=0;i<9;i++) { 
     buttons[i].setHeight((int) (h/3)); 
     buttons[i].setWidth((int) (w/3)); 
    } 
} 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    beginPlay(); 
    if (requestCode == ACTIVITY_SELECTION) { 
     if (resultCode == RESULT_OK) { 
      Bundle extras = data.getExtras(); 
      if (extras.getString("result").equals("CPU")) cpuPlay(); 
     } 
    } 
} 
private void beginPlay() { 
    //initializations start 
    w=new float[9]; 
    c=new int[9]; 
    InitTable(); 
    w[0]=0.7f; 
    w[1]=0.4f; 
    w[2]=0.7f; 
    w[3]=0.4f; 
    w[4]=0.7f; 
    w[5]=0.4f; 
    w[6]=0.7f; 
    w[7]=0.4f; 
    w[8]=0.7f; 
    //c[i] : 0 for empty, 1 for cpu, 2 for user 
    for(int i=0;i<9;i++) 
     c[i]=0; 
    //initializations done 

    //now we play! 
    for(int i=0;i<9;i++) 
     updateBtn(i); 
} 

private void cpuPlay() { 
    //computer plays first 
    int cpos=getDecision(); 
    if (cpos == -1) { 
     Toast toast = Toast.makeText(getApplicationContext(), "GAME OVER", Toast.LENGTH_SHORT); 
     toast.show(); 
     return; 
    } 
    c[cpos]=1; 
    updateBtn(cpos); 
    int gstatus = CheckGameStatus(); 
    if (gstatus == GAME_VICTORY) { 
     Toast toast = Toast.makeText(getApplicationContext(), "Congrts You Won", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
    else if (gstatus == GAME_DEFEAT) { 
     Toast toast = Toast.makeText(getApplicationContext(), "Sorry, You Lost", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
    else if (gstatus == GAME_TIE) { 
     Toast toast = Toast.makeText(getApplicationContext(), "Its a TIE, press reset to play again", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
    else if (gstatus == GAME_CONTINUES) { 
     //user plays 
    } 
} 
private void updateBtn(int i) { 
    if(c[i]==0) 
     buttons[i].setText(" "); 
    else if(c[i]==1) 
     buttons[i].setText("O"); 
    else 
     buttons[i].setText("X"); 
} 
private int CheckGameStatus() { 
    int s = 0; 
    //check horizontal 
    if(c[0]==2&&c[1]==2&&c[2]==2) {s = GAME_VICTORY;} 
    if(c[3]==2&&c[4]==2&&c[5]==2) {s = GAME_VICTORY;} 
    if(c[6]==2&&c[7]==2&&c[8]==2) {s = GAME_VICTORY;} 
    if(c[0]==1&&c[1]==1&&c[2]==1) {s = GAME_DEFEAT;} 
    if(c[3]==1&&c[4]==1&&c[5]==1) {s = GAME_DEFEAT;} 
    if(c[6]==1&&c[7]==1&&c[8]==1) {s = GAME_DEFEAT;} 
    //check vertical 
    if(c[0]==2&&c[3]==2&&c[6]==2) {s = GAME_VICTORY;} 
    if(c[1]==2&&c[4]==2&&c[7]==2) {s = GAME_VICTORY;} 
    if(c[2]==2&&c[5]==2&&c[8]==2) {s = GAME_VICTORY;} 
    if(c[0]==1&&c[3]==1&&c[6]==1) {s = GAME_DEFEAT;} 
    if(c[1]==1&&c[4]==1&&c[7]==1) {s = GAME_DEFEAT;} 
    if(c[2]==1&&c[5]==1&&c[8]==1) {s = GAME_DEFEAT;} 
    //check diagonal 
    if(c[0]==2&&c[4]==2&&c[8]==2) {s = GAME_VICTORY;} 
    if(c[2]==2&&c[4]==2&&c[6]==2) {s = GAME_VICTORY;} 
    if(c[0]==1&&c[4]==1&&c[8]==1) {s = GAME_DEFEAT;} 
    if(c[2]==1&&c[4]==1&&c[6]==1) {s = GAME_DEFEAT;} 

    if (s != 0) { 
     buttonsEnable(false); 
     return s; 
    } 

    boolean box_empty = false; 
    for(int i=0;i<9;i++) { 
     if (c[i] == 0) box_empty = true; 
    } 
    if (box_empty) { //if any box is empty -> game continues 
     return GAME_CONTINUES; 
    } 
    else { //else there is tie 
     buttonsEnable(false); 
     return GAME_TIE; 
    } 
} 

private void buttonsEnable(boolean b) { 
    for(int i=0;i<9;i++) 
     buttons[i].setEnabled(b); 
} 

private void btnClicked(int i) { 
    if(c[i]!=0) { 
     Toast toast = Toast.makeText(getApplicationContext(), "Position occupied", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
    else { 
     //all OK 
     c[i] = 2; 
     updateBtn(i); 
     int gstatus = CheckGameStatus(); 
     if (gstatus == GAME_VICTORY) { 
      Toast toast = Toast.makeText(getApplicationContext(), "Congrats You Won", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
     else if (gstatus == GAME_DEFEAT) { 
      Toast toast = Toast.makeText(getApplicationContext(), "Sorry, You Lost", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
     else if (gstatus == GAME_TIE) { 
      Toast toast = Toast.makeText(getApplicationContext(), "Its a TIE, press reset to play again", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
     else if (gstatus == GAME_CONTINUES) { 
      cpuPlay(); 
     } 
    } 
} 

private int getDecision() { 
    for(int i=0;i<9;i++) 
     for(int j=0;j<9;j++) { 
      if(c[i]==1&&c[j]==1) //place 'o' to win 
       if(PosTable[i][j]!=-1) //if we have 3 in a row 
        if(c[PosTable[i][j]]==0) //if position is free 
         return PosTable[i][j]; 
      if(c[i]==2&&c[j]==2) //place 'o' to prevent user's victory 
       if(PosTable[i][j]!=-1) //if we have 3 in a row 
        if(c[PosTable[i][j]]==0) //if position is free 
         return PosTable[i][j]; 
     } 

    if(c[0]==1&&c[8]==0) return 8; 
    if(c[2]==1&&c[6]==0) return 6; 
    if(c[8]==1&&c[0]==0) return 0; 
    if(c[6]==1&&c[2]==0) return 2; 
    Random r=new Random(); 
    boolean exist07=false; 
    boolean[] free=new boolean[9]; //will hold the free positions 
    for(int i=0;i<9;i++) 
     free[i]=false; 
    for(int i=0;i<9;i++) 
     if(c[i]==0) { //free ?? 
      free[i]=true; //add position to free 
      if(w[i]==UNIQUE_MAX_WEIGHT) return i; 
     } 
    //more than 1 positions with same weight 
    for(int i=0;i<9;i++) 
     if(free[i]) //if position is free 
      if(w[i]==0.7f) exist07=true; 
    if(exist07) 
     for(int i=0;i<9;i++) 
      if(free[i]) //if position is free 
       if(w[i]==0.4f) free[i]=false; 

    int j=0; 
    int rn=0; 
    int[] tmp; 
    for(int i=0;i<9;i++) 
     if(free[i]) j++; 
    if(j!=0) { 
     tmp=new int[j]; 
     rn=r.nextInt(j); 
     j=0; 
     for(int i=0;i<9;i++) 
      if(free[i]) tmp[j++]=i; 
     return tmp[rn]; 
    } 
    else { 
     return -1; //else GAME OVER 
    } 
} 
private void InitTable() { 

    PosTable=new int[9][9]; 
    for(int i=0;i<9;i++) 
     for(int j=0;j<9;j++) 
      PosTable[i][j]=-1; 
    PosTable[0][1]=2; 
    PosTable[0][2]=1; 
    PosTable[0][3]=6; 
    PosTable[0][4]=8; 
    PosTable[0][6]=3; 
    PosTable[0][8]=4; 
    PosTable[1][2]=0; 
    PosTable[1][4]=7; 
    PosTable[1][7]=4; 
    PosTable[2][4]=6; 
    PosTable[2][5]=8; 
    PosTable[2][6]=4; 
    PosTable[2][8]=5; 
    PosTable[3][4]=5; 
    PosTable[3][5]=4; 
    PosTable[3][6]=0; 
    PosTable[4][5]=3; 
    PosTable[4][6]=2; 
    PosTable[4][7]=1; 
    PosTable[4][8]=0; 
    PosTable[5][8]=2; 
    PosTable[6][7]=8; 
    PosTable[6][8]=7; 
    PosTable[7][8]=6; 

} 
+0

感謝所有誰試圖幫助,我終於得到了答案。那就是我必須改變startActivityForResult代碼的代碼,在那裏我必須編寫Game.class而不是TTTActivity.class。再次感謝朋友:) – 2012-04-17 09:23:49

回答

1

使用這個在您的Game.java

startActivityForResult(new Intent(Game.this, Game.class), ACTIVITY_SELECTION);

+0

感謝它爲我工作 – 2012-04-23 05:37:20

1

我發現這個在您的遊戲活動的onCreate()

startActivityForResult(new Intent(Game.this, TTTActivity.class), ACTIVITY_SELECTION); 

所以你有

TTTActivity ----點擊開始---->遊戲(活動) - 創建--->再次啓動TTTActivity!

你的遊戲不是顯示,但它只是馬上回來。

+0

實際上,如果您要評論此行,那麼整個應用程序無法正常工作。所有按鈕都顯示默認的假值,如果我點擊任何按鈕應用程序突然終止。所以我的高級建議我添加這一行。 – 2012-04-17 08:03:43

+0

默認的虛假值不應該與上面的代碼行有關,並且FC應該與其他錯誤有關,請仔細檢查logcat的結果。 – xandy 2012-04-17 08:38:10

0

添加此代碼...

Button newButton = (Button)findViewById(R.id.new_button); 
    newButton.setOnClickListener(this);   

    Button exitButton =(Button) findViewById(R.id.exit_button); 
    exitButton.setOnClickListener(this); 

,我不認爲你想在oncreate()

+0

我在哪裏添加此代碼?我不認爲它可以幫助我處理這個活動問題。請解釋清楚 – 2012-04-17 08:00:17

+0

@VikasGupta ..你的**的視圖newButton = findViewById(R.id.new_button); newButton.setOnClickListener(this); 查看exitButton = findViewById(R.id.exit_button); exitButton.setOnClickListener(this); ** – ngesh 2012-04-17 08:01:41

+0

實際上,如果我dnt添加該行然後整個應用程序無法正常工作。所有按鈕都顯示默認的假值,如果我點擊任何按鈕應用程序突然終止。所以我的高級建議我添加這一行。 – 2012-04-17 08:08:43

0

最後我得到它做'startActivityForResult(new Intent(Game.this, TTTActivity.class), ACTIVITY_SELECTION); 「,有曾在此代碼中的一些錯誤

startActivityForResult(new Intent(Game.this, TTTActivity.class), ACTIVITY_SELECTION); 

它應該是這樣的。

startActivityForResult(new Intent(Game.this, Game.class), ACTIVITY_SELECTION); 

實際上,它需要將活動從自身改變爲自身。所以它對我來說絕對很好。 BTW非常感謝所有想要幫助的人。 :)

相關問題