011.紅心A,HeartsA
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#808080"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="@string/hello_world"
android:textSize="30sp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="onClick"
android:src="@drawable/p0" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="onClick"
android:src="@drawable/p0" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="onClick"
android:src="@drawable/p0" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="onClick"
android:src="@drawable/p0" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:onClick="playAgain"
android:text="開始遊戲" />
</LinearLayout>
package com.example.app11;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageView[] imageViews = new ImageView[4];
Resources res;
int[] imageRandom = new int[4];
int[] imageIds = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4, };
Button btnPlayAgain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlayAgain = (Button) findViewById(R.id.button1);
res = this.getResources();
for (int i = 0; i < imageViews.length; i++) {
int id = res.getIdentifier("imageView" + (i + 1), "id", this.getPackageName());
imageViews[i] = (ImageView) this.findViewById(id);
}
startGame();
}
private void startGame() {
// TODO Auto-generated method stub
for (int i = 0; i < 4; i++) {
imageViews[i].setImageResource(R.drawable.p0);
imageViews[i].setClickable(true);
imageViews[i].setAlpha(255);
}
boolean[] flag = new boolean[4];// 四個狀態預設為false
for (int i = 0; i < 4;) {
int tmp = (int) (Math.random() * 4);// 0~3
if (flag[tmp] == false) {// 沒抓過
flag[tmp] = true;
imageRandom[i] = tmp;
if (tmp == 0) { // 如果是紅心A
imageViews[i].setTag("Yes");
} else {
imageViews[i].setTag("No");
}
i++;
}
}
}
public void onClick(View view) {
// ((ImageView)view).setImageResource(R.drawable.p1);
if ("Yes".equals(view.getTag())) {
Toast.makeText(this, "你猜對了", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "你猜錯了", Toast.LENGTH_SHORT).show();
}
for (int i = 0; i < 4; i++) {
imageViews[i].setImageResource(imageIds[imageRandom[i]]);
imageViews[i].setClickable(false);
if ("Yes".equals(imageViews[i].getTag())) {// 紅心A
imageViews[i].setAlpha(255);
} else {
imageViews[i].setAlpha(128);
}
}
btnPlayAgain.setEnabled(true);
}
public void playAgain(View view) {
startGame();
}
}
- 圖片資源: