Android Studio【小案例】——猜猜鸡蛋在哪只鞋子里?

效果图:
1
2

一、案例要求。

  • 了解实现的具体的流程
  • 怎样进行游戏界面的布局
  • ImageView组件的基本应用
  • 怎样实现随机指定鸡蛋所在的鞋子
  • 怎样设置ImageView组件的透明度

二、功能结构。

【猜猜鸡蛋在哪只鞋子里】——>【显示3只鞋子】——>【点击鞋子显示结果】——>【再玩一次】
当我们单击其中的任意一只鞋子,将打开鞋子,显示里面是否有鸡蛋,并且将没有被单击的鞋子设置为半透明显示,而单击的鞋子则正常显示,同时根据单击的鞋子里是否有鸡蛋显示对应的结果。例如,单击中间的鞋子,如果鸡蛋在这只鞋子里,将显示提示信息——【恭喜你猜中了】,否则,将显示——【很遗憾,猜错了】的提示信息。另外,当我们再点击再玩一次按钮时,可以重新进入游戏。

三、具体实现。

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background"
    tools:context="cn.edu.hznu.ex3_2.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:textSize="28sp"
        android:gravity="center"
        android:textColor="#FF0015FF"
        android:text="@string/title"
        android:id="@+id/textView"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:onClick="judge"
            android:layout_height="match_parent"
            android:src="@drawable/shoe_default"
            android:id="@+id/shoe1"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:onClick="judge"
            android:layout_height="match_parent"
            android:src="@drawable/shoe_default"
            android:id="@+id/shoe2"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:onClick="judge"
            android:layout_height="match_parent"
            android:src="@drawable/shoe_default"
            android:id="@+id/shoe3"/>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textSize="24sp"
        android:text="再玩一局"
        android:id="@+id/replay"/>
</LinearLayout>

实现代码:

package cn.edu.hznu.ex3_2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private int[] imagesId=new int[3];
    private  int position;
    private ImageView[] imageViews=new ImageView[3];
    private Button replay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.textView);
        imageViews[0]=(ImageView)findViewById(R.id.shoe1);
        imageViews[1]=(ImageView)findViewById(R.id.shoe2);
        imageViews[2]=(ImageView)findViewById(R.id.shoe3);
        replay=(Button)findViewById(R.id.replay);
        init();
        replay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(R.string.title); //又回到原来
                for(int i=0;i<3;i++){ //显示原来的鞋子
                    imageViews[i].setImageResource(R.drawable.shoe_default);
                    imageViews[i].setAlpha(1f);
                    imageViews[i].setEnabled(true);
                }
                init();
            }
        });
    }
    private void init() {
       position=(int) (Math.random()*3);
        for(int i=0;i<3;i++){
            if(i==position){
                imagesId[i]=R.drawable.shoe_ok;
            }else {
                imagesId[i]=R.drawable.shoe_sorry;
            }
        }
    }
    public void judge(View v){
//v代表点击的这个控件对象
        if(v==imageViews[position]){
           textView.setText("恭喜你猜中了!");
        }else{
        textView.setText("很遗憾,猜错了!");
        }
        for(int i=0;i<3;i++){
            imageViews[i].setImageResource(imagesId[i]);
            imageViews[i].setAlpha(0.5f);//控件的透明度
            imageViews[i].setEnabled(false);
        }
        v.setAlpha(1f);
    }
}

2020-3-19

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值