菜鸟Android学习之路15——猜猜鸡蛋在那个鞋子里



package com.example.rookie14;

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.ImageButton;
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;
 private ImageView image2;
 private ImageView image3;
 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));
   }
  });
 }
 
 private void isRight(View v,int index){
  image1.setImageDrawable(getResources().getDrawable(imageIds[0]));
  image2.setImageDrawable(getResources().getDrawable(imageIds[1]));
  image3.setImageDrawable(getResources().getDrawable(imageIds[2]));
  //为每个imageview设置半透明效果
  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(" you are great");
  }else{
   result.setText("you are bad");
  }
  
 }

 private void reset(){
  for(int i = 0;i<3;i++){
   int temp=imageIds[i];
   int indes=(int)(Math.random()*2);
   imageIds[i]=imageIds[indes];
   imageIds[indes]=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;
 }

}

你可能感兴趣的:(Android)