package com.example.prsgame; import - TopicsExpress



          

package com.example.prsgame; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //View findViews(); //Listeners setListeners(); } //宣告元件 private TextView txtResult; private ImageButton btnScissors1, btnStone1, btnNet1; private ImageView imgComPlay; //View private void findViews() { imgComPlay = (ImageView)findViewById(R.id.imgComPlay); txtResult = (TextView)findViewById(R.id.txtResult); btnScissors1 = (ImageButton)findViewById(R.id.btnScissors1); btnStone1 = (ImageButton)findViewById(R.id.btnStone1); btnNet1 = (ImageButton)findViewById(R.id.btnNet1); } //Control private void setListeners() { btnScissors1.setOnClickListener(btnScissors1Lin);//玩家出剪刀 btnStone1.setOnClickListener(btnStone1Lin);//玩家出石頭 btnNet1.setOnClickListener(btnNet1Lin);//玩家出布 } //Modules private Button.OnClickListener btnScissors1Lin = new Button.OnClickListener(){//玩家出剪刀 @Override public void onClick(View v) { //電腦出拳 int iComPlay = (int)(Math.random()*3+1); //1-剪刀,2-石頭,3-布 //顯示電腦出拳及判定輸贏 if (iComPlay == 1){//電腦出剪刀 //txtComPlay.setText(R.string.playScissors);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.scissor); txtResult.setText(getString(R.string.result)+getString(R.string.playerDraw));//顯示結果_平手 } else if (iComPlay == 2){//電腦出石頭 //txtComPlay.setText(R.string.playStone);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.rock); txtResult.setText(getString(R.string.result)+getString(R.string.playerLose));//顯示結果_玩家輸 } else {//電腦出布 //txtComPlay.setText(R.string.playNet);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.paper); txtResult.setText(getString(R.string.result)+getString(R.string.playerWin));//顯示結果_玩家贏 } } }; private Button.OnClickListener btnStone1Lin = new Button.OnClickListener(){//玩家出石頭 @Override public void onClick(View v) { //電腦出拳 int iComPlay = (int)(Math.random()*3+1); //1-剪刀,2-石頭,3-布 //顯示電腦出拳及判定輸贏 if (iComPlay == 1){//電腦出剪刀 //txtComPlay.setText(R.string.playScissors);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.scissor); txtResult.setText(getString(R.string.result)+getString(R.string.playerWin));//顯示結果_玩家贏 } else if (iComPlay == 2){//電腦出石頭 //txtComPlay.setText(R.string.playStone);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.rock); txtResult.setText(getString(R.string.result)+getString(R.string.playerDraw));//顯示結果_平手 } else {//電腦出布 //txtComPlay.setText(R.string.playNet);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.paper); txtResult.setText(getString(R.string.result)+getString(R.string.playerLose));//顯示結果_玩家輸 } } }; private Button.OnClickListener btnNet1Lin = new Button.OnClickListener(){//玩家出布 @Override public void onClick(View v) { //電腦出拳 int iComPlay = (int)(Math.random()*3+1); //1-剪刀,2-石頭,3-布 //顯示電腦出拳及判定輸贏 if (iComPlay == 1){//電腦出剪刀 //txtComPlay.setText(R.string.playScissors);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.scissor); txtResult.setText(getString(R.string.result)+getString(R.string.playerLose));//顯示結果_玩家輸 } else if (iComPlay == 2){//電腦出石頭 //txtComPlay.setText(R.string.playStone);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.rock); txtResult.setText(getString(R.string.result)+getString(R.string.playerWin));//顯示結果_玩家贏 } else {//電腦出布 //txtComPlay.setText(R.string.playNet);//顯示電腦出拳 imgComPlay.setImageResource(R.drawable.paper); txtResult.setText(getString(R.string.result)+getString(R.string.playerDraw));//顯示結果_平手 } } }; }
Posted on: Wed, 22 Oct 2014 09:48:17 +0000

Trending Topics



Recently Viewed Topics




© 2015