android-Image与Button结合的小游戏


老规矩。右键取得显示不出来的图片地址,利用下载工具下载这个图片。后缀改为rar即可得到源代码项目。


package zhang.game;

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 hongtaoA extends Activity {
	private ImageView mimageView01;
	private ImageView mimageView02;
	private ImageView mimageView03;
	private Button mButton;
	private TextView mText;
	
	private int[] s1={R.drawable.p01,R.drawable.p02,R.drawable.p03};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mimageView01=(ImageView)findViewById(R.id.mImage01);
        mimageView02=(ImageView)findViewById(R.id.mImage02);
        mimageView03=(ImageView)findViewById(R.id.mImage03);
        mButton =(Button)findViewById(R.id.mButton);
        mText=(TextView)findViewById(R.id.mText);
        

        
        mimageView01.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mimageView01.setImageDrawable(getResources().getDrawable(s1[0]));
				mimageView02.setImageDrawable(getResources().getDrawable(s1[1]));
				mimageView03.setImageDrawable(getResources().getDrawable(s1[2]));
				
				mimageView03.setAlpha(100);
				mimageView02.setAlpha(100);
				// TODO Auto-generated method stub
				if(s1[0]==R.drawable.p01){
					mText.setText(" ok");
				}else{
					mText.setText("error");
				}
			}
		});
        
        mimageView02.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mimageView01.setImageDrawable(getResources().getDrawable(s1[0]));
				mimageView02.setImageDrawable(getResources().getDrawable(s1[1]));
				mimageView03.setImageDrawable(getResources().getDrawable(s1[2]));
				
				mimageView03.setAlpha(100);
				mimageView01.setAlpha(100);
				// TODO Auto-generated method stub
				if(s1[1]==R.drawable.p01){
					mText.setText(" ok");
				}else{
					mText.setText("error");
				}
			}
		});  
        mimageView03.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mimageView01.setImageDrawable(getResources().getDrawable(s1[0]));
				mimageView02.setImageDrawable(getResources().getDrawable(s1[1]));
				mimageView03.setImageDrawable(getResources().getDrawable(s1[2]));
				
				mimageView01.setAlpha(100);
				mimageView02.setAlpha(100);
				// TODO Auto-generated method stub
				if(s1[2]==R.drawable.p01){
					mText.setText(" ok");
				}else{
					mText.setText("error");
				}
			}
		});
        mButton.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mText.setText(R.string.str_title);
				mimageView01.setImageDrawable(getResources().getDrawable(R.drawable.p04));
				mimageView02.setImageDrawable(getResources().getDrawable(R.drawable.p04));
				mimageView03.setImageDrawable(getResources().getDrawable(R.drawable.p04));
				mimageView01.setAlpha(255);
				mimageView02.setAlpha(255);
				mimageView03.setAlpha(255);
				randon();
				
			}});
    }
    private void randon(){
    	for(int i=0;i<3;i++){
    		int tmp=s1[i];
    		int s=(int)(Math.random()*2);
    		s1[i]=s1[s];
    		s1[s]=tmp;
    	}
    	
    }
    
}




<?xml version="1.0" encoding="utf-8"?>
  <AbsoluteLayout
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
  >
    <TextView
      android:id="@+id/mText"
      android:layout_width="270px"
      android:layout_height="40px"
      android:text="@string/str_title"
      android:textSize="18sp"
      android:layout_x="20px"
      android:layout_y="32px"
    >
    </TextView>
    <ImageView
      android:id="@+id/mImage01"
      android:layout_width="71px"
      android:layout_height="96px"
      android:layout_x="20px"
      android:layout_y="122px"
      android:src="@drawable/p04"
    >
    </ImageView>
    <ImageView
      android:id="@+id/mImage02"
      android:layout_width="71px"
      android:layout_height="96px"
      android:layout_x="126px"
      android:layout_y="122px"
      android:src="@drawable/p04"
    >
    </ImageView>
    <ImageView
      android:id="@+id/mImage03"
      android:layout_width="71px"
      android:layout_height="96px"
      android:layout_x="232px"
      android:layout_y="122px"
      android:src="@drawable/p04"
    >
    </ImageView>
    <Button
      android:id="@+id/mButton"
      android:layout_width="118px"
      android:layout_height="wrap_content"
      android:text="@string/str_button"
      android:layout_x="100px"
      android:layout_y="302px"
    >
    </Button>
  </AbsoluteLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值