java:
package EX04_16.txt;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class EX04_16 extends Activity {
private TextView mTextView;
private Button mButton;
private ImageView mPock1;
private ImageView mPock2;
private ImageView mPock3;
private static int[] s1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initGame();
startGame();
}
private void startGame() {
// TODO Auto-generated method stub
randon();
mPock1.setOnClickListener(new ImageView.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPock1.setBackgroundDrawable(getResources().getDrawable(s1[0]));
mPock2.setBackgroundDrawable(getResources().getDrawable(s1[1]));
mPock3.setBackgroundDrawable(getResources().getDrawable(s1[2]));
mPock2.setAlpha(100);
mPock3.setAlpha(100);
if (s1[0] == R.drawable.coffer) {
mTextView.setText("哇!你猜到了!!拍拍手!");
} else {
mTextView.setText("你猜错了喔!!要不要再试一次?");
}
}
});
mPock2.setOnClickListener(new ImageView.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPock1.setBackgroundDrawable(getResources().getDrawable(s1[0]));
mPock2.setBackgroundDrawable(getResources().getDrawable(s1[1]));
mPock3.setBackgroundDrawable(getResources().getDrawable(s1[2]));
mPock1.setAlpha(100);
mPock3.setAlpha(100);
if (s1[1] == R.drawable.coffer) {
mTextView.setText("哇!你猜到了!!拍拍手!");
} else {
mTextView.setText("你猜错了喔!!要不要再试一次?");
}
}
});
mPock3.setOnClickListener(new ImageView.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPock1.setBackgroundDrawable(getResources().getDrawable(s1[0]));
mPock2.setBackgroundDrawable(getResources().getDrawable(s1[1]));
mPock3.setBackgroundDrawable(getResources().getDrawable(s1[2]));
mPock2.setAlpha(100);
mPock1.setAlpha(100);
if (s1[2] == R.drawable.coffer) {
mTextView.setText("哇!你猜到了!!拍拍手!");
} else {
mTextView.setText("你猜错了喔!!要不要再试一次?");
}
}
});
mButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPock1.setBackgroundDrawable(getResources().getDrawable(R.drawable.apple));
mPock2.setBackgroundDrawable(getResources().getDrawable(R.drawable.apple));
mPock3.setBackgroundDrawable(getResources().getDrawable(R.drawable.apple));
mPock3.setAlpha(255);
mPock2.setAlpha(255);
mPock1.setAlpha(255);
randon();
}
});
}
private void randon() {
// TODO Auto-generated method stub
int tem, t;
for (int i = 0; i < s1.length; i++) {
tem = s1[i];
t = (int) (Math.random()*2);
s1[i] = s1[t];
s1[t] = tem;
}
}
private void initGame() {
// TODO Auto-generated method stub
mTextView = (TextView)findViewById(R.id.myTextView);
mButton = (Button)findViewById(R.id.myButton);
mPock1 = (ImageView)findViewById(R.id.myPock1);
mPock2 = (ImageView)findViewById(R.id.myPock2);
mPock3 = (ImageView)findViewById(R.id.myPock3);
s1 = new int[]{R.drawable.coffer, R.drawable.rain, R.drawable.sun};
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/myTextView"
android:layout_marginTop = "20px"
android:layout_marginLeft = "20px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "猜猜咖啡是哪一张?"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id = "@+id/myPock1"
android:layout_marginTop = "50px"
android:layout_marginLeft = "20px"
android:layout_width="80px"
android:layout_height="80px"
android:background = "@drawable/apple"
/>
<ImageView
android:id = "@+id/myPock2"
android:layout_marginTop = "50px"
android:layout_marginLeft = "20px"
android:layout_width="80px"
android:layout_height="80px"
android:background = "@drawable/apple"
/>
<ImageView
android:id = "@+id/myPock3"
android:layout_marginTop = "50px"
android:layout_marginLeft = "20px"
android:layout_width="80px"
android:layout_height="80px"
android:background = "@drawable/apple"
/>
</LinearLayout>
<Button
android:id = "@+id/myButton"
android:layout_marginTop = "100px"
android:layout_gravity = "center"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text = "再玩一次"
/>
</LinearLayout>