標籤: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>