package com.example.caijidan;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	int[] imageIds = new int[] { R.drawable.shoe_ok, R.drawable.shoe_sorry,
			R.drawable.shoe_sorry };
	private ImageView image1;		//ImageView组件1
	private ImageView image2;		//ImageView组件2
	private ImageView image3;		//ImageView组件3
	private TextView result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		image1 = (ImageView) findViewById(R.id.imageView1);
		image2 = (ImageView) findViewById(R.id.imageView2);
		image3 = (ImageView) findViewById(R.id.imageView3);
		result = (TextView) findViewById(R.id.textView1);
		reset();
		image1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isRight(v, 0);
			}
			
		});
		image2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isRight(v, 1);
			}
			
		});
		image3.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isRight(v, 2);
			}
			
		});
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				reset();
				result.setText(R.string.title);
				image1.setAlpha(255);
				image2.setAlpha(255);
				image3.setAlpha(255);
				image1.setImageDrawable(getResources().getDrawable(
						R.drawable.shoe_default));
				image2.setImageDrawable(getResources().getDrawable(
						R.drawable.shoe_default));
				image3.setImageDrawable(getResources().getDrawable(
						R.drawable.shoe_default));
			}
			
		});
	}

	protected void isRight(View v, int index) {
		// TODO Auto-generated method stub
		image1.setImageDrawable(getResources().getDrawable(imageIds[0]));
		image2.setImageDrawable(getResources().getDrawable(imageIds[1]));
		image3.setImageDrawable(getResources().getDrawable(imageIds[2]));
		image1.setAlpha(100);
		image2.setAlpha(100);
		image3.setAlpha(100);
		ImageView v1 = (ImageView) v;
		v1.setAlpha(255);
		if (imageIds[index] == R.drawable.shoe_ok){
			result.setText("恭喜您,猜对了,祝你幸福!");
		} else {
			result.setText("很抱歉,猜错了,要不要再试一次?");
		}
	}

	private void reset() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 3; i++) {
			int temp = imageIds[i];				//将数组元素i保存到临时变量中
			int index = (int) (Math.random() * 2);		//生成一个随机数
			imageIds[i] = imageIds[index];			//将随机数指定的数组元素的内容赋值给数组元素i
			imageIds[index] = temp;
	     }
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}