蝸牛—Android基礎之簡易猜拳遊戲

來源:互聯網
上載者:User

標籤:des   android   class   code   java   http   

MainActivity.java

package org.example.guess;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity {private ImageButton r_imgBtn, p_imgBtn, s_imgBtn; // 石頭、布、剪刀的按鈕private ImageView imgView; // 遊戲介面上面的按鈕private TextView reslut_tv, coun_tv; // 結果及遊戲次數的textViewint count = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);r_imgBtn = (ImageButton) findViewById(R.id.btnRock);p_imgBtn = (ImageButton) findViewById(R.id.btnPaper);s_imgBtn = (ImageButton) findViewById(R.id.btnSci);imgView = (ImageView) findViewById(R.id.viewCmp);reslut_tv = (TextView) findViewById(R.id.textResult);coun_tv = (TextView) findViewById(R.id.textCount);MyOnClickListener myOnClickListener = new MyOnClickListener();r_imgBtn.setOnClickListener(myOnClickListener);p_imgBtn.setOnClickListener(myOnClickListener);s_imgBtn.setOnClickListener(myOnClickListener);}private class MyOnClickListener implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint rand = (int) (Math.random() * 3 + 1); // 得到1~3的隨機數count++;// 遊戲次數++switch (rand) {/** * 當rand=1時,即電腦出的是石頭,然後再判斷使用者出的什麼。 */case 1:imgView.setImageResource(R.drawable.rock);switch (v.getId()) {case R.id.btnRock:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_draw));coun_tv.setText("遊戲次數:" + count);break;case R.id.btnPaper:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_lose));coun_tv.setText("遊戲次數:" + count);break;case R.id.btnSci:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_win));coun_tv.setText("遊戲次數:" + count);break;}break;case 2:imgView.setImageResource(R.drawable.paper);switch (v.getId()) {case R.id.btnRock:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_win));coun_tv.setText("遊戲次數:" + count);break;case R.id.btnPaper:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_draw));coun_tv.setText("遊戲次數:" + count);break;case R.id.btnSci:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_lose));coun_tv.setText("遊戲次數:" + count);break;}break;case 3:imgView.setImageResource(R.drawable.scissors);switch (v.getId()) {case R.id.btnRock:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_lose));coun_tv.setText("遊戲次數:" + count);break;case R.id.btnPaper:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_win));coun_tv.setText("遊戲次數:" + count);break;case R.id.btnSci:reslut_tv.setText(getString(R.string.result)+ getString(R.string.result_draw));coun_tv.setText("遊戲次數:" + count);break;}break;}}}}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/welcome"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/fg_title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:src="@drawable/fg_title" />    <TextView        android:id="@+id/line"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/fg_title"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:marqueeRepeatLimit="marquee_forever"        android:singleLine="true"        android:text="@string/notification"        android:textSize="10sp" />    <TextView        android:id="@+id/cmpShow"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/line"        android:layout_marginLeft="20dp"        android:text="@string/cmpShow"        android:textColor="#ff0000" />    <TextView        android:id="@+id/playerShow"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@id/line"        android:layout_marginRight="20dp"        android:text="@string/playerShow"        android:textColor="#ff0000" />    <ImageButton        android:id="@+id/btnRock"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@id/playerShow"        android:layout_below="@id/playerShow"        android:layout_marginRight="20dp"        android:layout_marginTop="9dp"        android:contentDescription="@string/rockDes"        android:src="@drawable/rock" />    <ImageButton        android:id="@+id/btnPaper"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@id/btnRock"        android:layout_below="@id/btnRock"        android:layout_marginRight="20dp"        android:layout_marginTop="9dp"        android:contentDescription="@string/paperDes"        android:src="@drawable/paper" />    <ImageView        android:id="@+id/viewCmp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@id/btnPaper"        android:layout_below="@id/cmpShow"        android:layout_marginLeft="20dp"        android:contentDescription="@string/cmpViewDes" />    <ImageButton        android:id="@+id/btnSci"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@id/btnPaper"        android:layout_below="@id/btnPaper"        android:layout_marginRight="20dp"        android:layout_marginTop="9dp"        android:contentDescription="@string/scissorsDes"        android:src="@drawable/scissors" />    <TextView        android:id="@+id/textResult"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/viewCmp"        android:layout_alignParentBottom="true"        android:layout_marginBottom="14dp"        android:text="@string/result"        android:textColor="#ff6ec7"        android:textSize="15sp" />    <TextView        android:id="@+id/textCount"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textResult"        android:layout_alignParentBottom="true"        android:text="遊戲次數:"        android:textColor="#ff2400"        android:textSize="15sp" /></RelativeLayout>

WelcomeActivity.java

package org.example.guess;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class WelcomeActivity extends Activity {/* * 遊戲開始介面 、、、即等待1.5s後進入遊戲介面 */@Overridepublic void onCreate(Bundle bundle) {super.onCreate(bundle);super.setContentView(R.layout.welcome);Thread splashTread = new Thread() {@Overridepublic void run() {try {sleep(1500);// 這裡進行操作} catch (InterruptedException e) {e.printStackTrace();} finally {finish();// 調用這個Activity 的finish方法後,在主介面按手機 上的放回鍵就會直接退出程式// 啟動主應用Intent intent = new Intent();intent.setClass(WelcomeActivity.this, MainActivity.class);startActivity(intent);}}};splashTread.start();}}

welcome.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:src="@drawable/welcome"         android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.