android小程序--猜猜红桃A在哪儿

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>


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值