EX04_16.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:5k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex04_16;
  2. /* import相关class */
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.Button;
  6. import android.widget.ImageView;
  7. import android.widget.TextView;
  8. import android.view.View;
  9. public class EX04_16 extends Activity
  10. {
  11.   /*声明对象变量*/
  12.   private ImageView mImageView01;
  13.   private ImageView mImageView02;
  14.   private ImageView mImageView03;
  15.   private Button mButton;
  16.   private TextView mText;
  17.   /*声明长度为3的int数组,并将三张牌的id放入
  18.     R.drawable.p01:红心A
  19.     R.drawable.p02:黑桃2
  20.     R.drawable.p03:梅花3
  21.     R.drawable.p04:扑克牌背面*/
  22.   private static int[] s1=
  23.     new int[]{R.drawable.p01,R.drawable.p02,R.drawable.p03};
  24.   
  25.   /** Called when the activity is first created. */
  26.   @Override
  27.   public void onCreate(Bundle savedInstanceState)
  28.   {
  29.     super.onCreate(savedInstanceState);
  30.     /* 载入main.xml Layout */
  31.     setContentView(R.layout.main);
  32.     /* 取得相关对象 */
  33.     mText=(TextView)findViewById(R.id.mText);
  34.     mImageView01=(ImageView)findViewById(R.id.mImage01);
  35.     mImageView02=(ImageView)findViewById(R.id.mImage02);
  36.     mImageView03=(ImageView)findViewById(R.id.mImage03);
  37.     mButton=(Button)findViewById(R.id.mButton);
  38.     /* 运行洗牌程序 */
  39.     randon();
  40.     /* 替mImageView01添加onClickListener*/
  41.     mImageView01.setOnClickListener(new View.OnClickListener()
  42.     {
  43.       public void onClick(View v)
  44.       {
  45.         /* 三张牌同时翻面,并将未选择的两张牌变透明 */
  46.         mImageView01.setImageDrawable(getResources()
  47.                             .getDrawable(s1[0]));
  48.         mImageView02.setImageDrawable(getResources()
  49.                             .getDrawable(s1[1]));
  50.         mImageView03.setImageDrawable(getResources()
  51.                             .getDrawable(s1[2]));
  52.         mImageView02.setAlpha(100);
  53.         mImageView03.setAlpha(100);
  54.         /* 依有没有猜对来决定TextView要显示的信息 */
  55.         if(s1[0]==R.drawable.p01)
  56.         {
  57.           mText.setText("哇!你猜对了喔!!拍拍手!");
  58.         }
  59.         else
  60.         {
  61.           mText.setText("你猜错了喔!!要不要再试一次?");
  62.         }
  63.       }
  64.     });
  65.     
  66.     /* 替mImageView02添加onClickListener*/
  67.     mImageView02.setOnClickListener(new View.OnClickListener()
  68.     {
  69.       public void onClick(View v)
  70.       {
  71.         /* 三张牌同时翻面,并将未选择的两张牌变透明 */
  72.         mImageView01.setImageDrawable(getResources()
  73.                             .getDrawable(s1[0]));
  74.         mImageView02.setImageDrawable(getResources()
  75.                             .getDrawable(s1[1]));
  76.         mImageView03.setImageDrawable(getResources()
  77.                             .getDrawable(s1[2]));
  78.         mImageView01.setAlpha(100);
  79.         mImageView03.setAlpha(100);
  80.         /* 依有没有猜对来决定TextView要显示的信息 */
  81.         if(s1[1]==R.drawable.p01)
  82.         {
  83.           mText.setText("哇!你猜对了喔!!拍拍手!");
  84.         }
  85.         else
  86.         {
  87.           mText.setText("你猜错了喔!!要不要再试一次?");
  88.         }
  89.       }
  90.     });
  91.     
  92.     /* 替mImageView03添加onClickListener*/
  93.     mImageView03.setOnClickListener(new View.OnClickListener()
  94.     {
  95.       public void onClick(View v)
  96.       {
  97.         /* 三张牌同时翻面,并将未选择的两张牌变透明 */
  98.         mImageView01.setImageDrawable(getResources()
  99.                             .getDrawable(s1[0]));
  100.         mImageView02.setImageDrawable(getResources()
  101.                             .getDrawable(s1[1]));
  102.         mImageView03.setImageDrawable(getResources()
  103.                             .getDrawable(s1[2]));
  104.         mImageView01.setAlpha(100);
  105.         mImageView02.setAlpha(100);
  106.         /* 依有没有猜对来决定TextView要显示的信息 */
  107.         if(s1[2]==R.drawable.p01)
  108.         {
  109.           mText.setText("哇!你猜对了喔!!拍拍手!");
  110.         }
  111.         else
  112.         {
  113.           mText.setText("你猜错了喔!!要不要再试一次?");
  114.         }
  115.       }
  116.     });
  117.     
  118.     /* 点击Button后三张牌都翻为背面且重新洗牌*/   
  119.     mButton.setOnClickListener(new Button.OnClickListener()
  120.     {
  121.       public void onClick(View v)
  122.       {
  123.         mText.setText("猜猜看红心A是哪一张?");
  124.         mImageView01.setImageDrawable(getResources()
  125.                 .getDrawable(R.drawable.p04));
  126.         mImageView02.setImageDrawable(getResources()
  127.                 .getDrawable(R.drawable.p04));
  128.         mImageView03.setImageDrawable(getResources()
  129.                 .getDrawable(R.drawable.p04));
  130.         mImageView01.setAlpha(255);
  131.         mImageView02.setAlpha(255);
  132.         mImageView03.setAlpha(255);
  133.         randon();
  134.       }
  135.     });    
  136.   }
  137.   /*重新洗牌的程序*/
  138.   private void randon()
  139.   {
  140.     for(int i=0;i<3;i++)
  141.     {
  142.       int tmp=s1[i];
  143.       int s=(int)(Math.random()*2);
  144.       s1[i]=s1[s];
  145.       s1[s]=tmp;
  146.     }
  147.   }
  148. }