安卓app_sl3_26练习_猜猜鸡蛋在哪只鞋子里面

安卓app_sl3_26练习_猜猜鸡蛋在哪只鞋子里面

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:id="@+id/tableLayout1"
    
    tools:context="com.example.sl3_26.MainActivity" >
    <TableRow android:id="@+id/tableRow1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="#010D18"
            android:text="@string/title" />
        
    </TableRow>
    <TableRow android:id="@+id/tableRow2" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        android:gravity="center_horizontal"
        >

             <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

             <ImageView
            android:id="@+id/imageView1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:src="@drawable/shoe_default" />
            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:src="@drawable/shoe_default" />
             <ImageView
                android:id="@+id/imageView3"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:src="@drawable/shoe_default" />
            

        </LinearLayout>

    </TableRow>
    <TableRow android:id="@+id/tableRow3" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:orientation="horizontal"
            android:gravity="center_horizontal">

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="再来一次" />
             
        </LinearLayout>

    </TableRow>
    

</TableLayout>
package com.example.sl3_26;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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;
    private ImageView image2;
    private ImageView image3;
    private TextView result;
    private void reset()
    {
        for(int i=0;i<3;i++)
        {
            int temp=imageIds[i];
            int index=(int)(Math.random()*2);
            imageIds[i]=imageIds[index];
            imageIds[index]=temp;
        }
    }
    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]));
        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("打错了,再来一次?");
        }
    }
    @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 自动生成的方法存根
                isRight(v,0);
                
            }
            
        });
        image2.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                isRight(v,1);
                
            }
            
        });
        image3.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                isRight(v,2);
                
            }
            
        });
        Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                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));
                
            }
            
        });
        
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">sl3_26</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="title">猜猜鸡蛋在哪个鞋子里?</string>

</resources>

 

posted @ 2022-02-20 16:38  txwtech  阅读(50)  评论(0编辑  收藏  举报