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; // 结果及游戏次数的textView int count = 0; @Override protected 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 { @Override public void onClick(View v) { // TODO Auto-generated method stub int 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后进入游戏界面 */ @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); super.setContentView(R.layout.welcome); Thread splashTread = new Thread() { @Override public 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>