package com.chaowen; import android.app.Activity; import android.os.Bundle; import android.text.StaticLayout; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class Ex04_17_ImageView extends Activity { /** Called when the activity is first created. */ private ImageView i1; private ImageView i2; private ImageView i3; private Button b1; private TextView t1; //存入三张牌的ID private static int[] ss=new int[]{R.drawable.p01,R.drawable.p02,R.drawable.p03}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); i1=(ImageView)findViewById(R.id.mImage01); i2=(ImageView)findViewById(R.id.mImage02); i3=(ImageView)findViewById(R.id.mImage03); b1=(Button)findViewById(R.id.mButton); t1=(TextView)findViewById(R.id.mText); //开始洗牌 randon(); i1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //三张牌同 时翻面,并将未选择的两张变透明 i1.setImageDrawable(getResources().getDrawable(ss[0])); i2.setImageDrawable(getResources().getDrawable(ss[1])); i3.setImageDrawable(getResources().getDrawable(ss[2])); i2.setAlpha(100); i3.setAlpha(100); //判断有没猜对 if(ss[0]==R.drawable.p01){ t1.setText("恭喜,你猜对了,拍拍手!!!"); }else { t1.setText("猜错啦,再来一次吧!!!"); } } }); i2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //三张牌同 时翻面,并将未选择的两张变透明 i1.setImageDrawable(getResources().getDrawable(ss[0])); i2.setImageDrawable(getResources().getDrawable(ss[1])); i3.setImageDrawable(getResources().getDrawable(ss[2])); i1.setAlpha(100); i3.setAlpha(100); //判断有没猜对 if(ss[1]==R.drawable.p01){ t1.setText("恭喜,你猜对了,拍拍手!!!"); }else { t1.setText("猜错啦,再来一次吧!!!"); } } }); i2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //三张牌同 时翻面,并将未选择的两张变透明 i1.setImageDrawable(getResources().getDrawable(ss[0])); i2.setImageDrawable(getResources().getDrawable(ss[1])); i3.setImageDrawable(getResources().getDrawable(ss[2])); i1.setAlpha(100); i2.setAlpha(100); //判断有没猜对 if(ss[2]==R.drawable.p01){ t1.setText("恭喜,你猜对了,拍拍手!!!"); }else { t1.setText("猜错啦,再来一次吧!!!"); } } }); b1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { t1.setText("猜猜看,红心A是哪一张?"); i1.setImageDrawable(getResources().getDrawable(R.drawable.p04)); i2.setImageDrawable(getResources().getDrawable(R.drawable.p04)); i3.setImageDrawable(getResources().getDrawable(R.drawable.p04)); i1.setAlpha(255); i2.setAlpha(255); i3.setAlpha(255); randon(); } }); } private void randon() { for(int i=0;i<3;i++) { int tmp=ss[i]; int s=(int)(Math.random()*2); ss[i]=ss[s]; ss[s]=tmp; } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout 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/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>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="hello">Hello World, EX04_16</string> <string name="app_name">EX04_16</string> <string name="str_title">猜猜看紅心A是哪一張?</string> <string name="str_button">再玩一次</string> </resources>