微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

安卓开发-老虎机案例

老虎机效果

这里写图片描述

1.src/
MainActivity.java

package com.example.tigerdemo3;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ImageView[] images = new ImageView[8];
	private TextView tv_coin;
	private int coin = 6666;// 总金额

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		images[0] = (ImageView) findViewById(R.id.img0);
		images[1] = (ImageView) findViewById(R.id.img1);
		images[2] = (ImageView) findViewById(R.id.img2);
		images[3] = (ImageView) findViewById(R.id.img3);
		images[4] = (ImageView) findViewById(R.id.img4);
		images[5] = (ImageView) findViewById(R.id.img5);
		images[6] = (ImageView) findViewById(R.id.img6);
		images[7] = (ImageView) findViewById(R.id.img7);
		tv_coin = (TextView) findViewById(R.id.tv_coin);

	}

	private static final int REQUEST_CODE = 9527;// 定义请求码
	// 用意图跳到SecondActivity.java

	public void btn1(View view) {
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, SecondActivity.class);
		startActivityForResult(intent, REQUEST_CODE);

	}

	private int currentPos = 0;// 当前位置
	private int selectedPos = -1;// 用户选择的位置
	private int selectedMoney = 0;// 初始化的金额

	@Override
	// 重写方法来接收传来的数据
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
			selectedPos = data.getIntExtra("pos", 0);
			selectedMoney = data.getIntExtra("money", 0);
			Toast.makeText(this, "money" + selectedMoney + "," + "pos" + selectedPos, Toast.LENGTH_LONG).show();

		}

	}

	Handler handler = new Handler() {
		@Override
		// 开始机制
		public void handleMessage(Message msg) {
			if (msg.what == 2002) {
				moveNext(); // 隔100毫秒跳一格
				handler.sendEmptyMessageDelayed(2002, 100);
			}
		}

	};

	// 利用selectedPosd来判断是否下注
	public void btn2(View view) {
		if (selectedPos < 0) {
			Toast.makeText(this, "未下注,请下注", Toast.LENGTH_LONG).show();
			return;
		}
		// 产生随机数
		Random random = new Random();
		int time = random.nextInt(2000) + 3000;
		// 停止机制
		handler.sendEmptyMessage(2002);
		tv_coin.postDelayed(new Runnable() {

			@Override
			public void run() {
				// Todo Auto-generated method stub
				handler.removeMessages(2002);// 移除ID2002让开始机制不能运转
				handleResult();// 调用handleResult()方法,判断是否中奖
			}
		}, time);

	}

	// 自定义移动方法
	public void moveNext() {
		currentPos = (currentPos + 1) % 8;// 0到7循环
		// 用于循环利用当前位置下标与i,再引用背景资源
		for (int i = 0; i < images.length; i++) {
			if (currentPos == i)
				images[i].setBackgroundResource(R.drawable.shape);
			else
				images[i].setBackgroundColor(Color.TRANSPARENT);

		}
	}

	// 自定义中奖方法
	public void handleResult() {
		if (currentPos == selectedPos) {
			// 中奖的
			coin = (int) (coin + (selectedMoney * SecondActivity.RATES[selectedPos]));
			Toast.makeText(this, "恭喜中奖", Toast.LENGTH_LONG).show();
		} else {
			// 不中奖的
			coin = coin - selectedMoney;
			Toast.makeText(this, "未中奖", Toast.LENGTH_LONG).show();
		}
		// 重新设置金币和初始化用户位置
		tv_coin.setText("金币:" + coin + "");
		selectedPos = -1;

	}

}

SecondActivity.java

package com.example.tigerdemo3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class SecondActivity extends Activity {
	// 初始化位置
	private int pos = -1;

	private ListView listview;

	// 适配器
	private SimpleAdapter adapter;
	// 定义 图片资源
	public static final int[] IMG = { R.drawable.img0, R.drawable.img1, R.drawable.img2, R.drawable.img3,
			R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, };
	// 定义人名
	public static final String[] NAME = { "甄姬", "黄月秋", "孙尚香", "小乔", "大乔", "蔡文姬", "张春华", "步练师" };
	// 定义赔率
	public static final Double[] RATES = { 5.0, 4.0, 3.5, 4.5, 5.0, 4.0, 3.5, 4.5 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		listview = (ListView) findViewById(R.id.listview);
		// 参数一 联系上下文 类名.this
		// 参数二 数据源为List<Map<String,Object>>类型
		// 参数三 自定义布局资源ID
		// 参数四 from字符串数组 数据来自Map中的key
		// 参数五 to 整型数组 自定义布局的控件ID
		adapter = new SimpleAdapter(SecondActivity.this, data(), R.layout.item, new String[] { "img", "name", "rate" },
				new int[] { R.id.imageview, R.id.textview1, R.id.textview2 });
		listview.setAdapter(adapter);

		// 确定item 的下标
		listview.setonItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// Todo Auto-generated method stub
				pos = position;
			}
		});
	}

	private List<Map<String, Object>> data() {
		// 创建集合来储存数据源
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		// 数据循环添加
		for (int i = 0; i < NAME.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("img", IMG[i]);
			map.put("name", NAME[i]);
			map.put("rate", RATES[i]);
			list.add(map);

		}
		return list;

	}

	// 判断是否下注
	public void btnConfirm(View view) {
		if (pos < 0) {
			Toast.makeText(this, "请下注", Toast.LENGTH_LONG).show();
			return;

		}
		// 确定金额
		RadioGroup group = (RadioGroup) findViewById(R.id.group);
		int chechedid = group.getCheckedRadioButtonId();
		RadioButton button = (RadioButton) findViewById(chechedid);
		String selecteMoney = button.getText().toString();
		int money = Integer.parseInt(selecteMoney);

		// 回传数据
		Intent data = new Intent();
		data.putExtra("money", money);
		data.putExtra("pos", pos);
		setResult(RESULT_OK, data);

		finish();
	}

}

2.res/layout/*

activity_main.xml

<LinearLayout 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="#222"
    android:orientation="vertical"
    android:weightSum="17"
    tools:context="com.example.tigerdemo3.MainActivity" >
	<!-- 第一个xml界面制作 -->
	
    <!-- 第一行 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center"
         >

        <ImageView
            android:id="@+id/img1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:src="@drawable/img0"
            android:background="@drawable/shape" />

        <ImageView
            android:id="@+id/img2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:src="@drawable/img1" />

        <ImageView
            android:id="@+id/img3"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:src="@drawable/img2" />
    </LinearLayout>

    <!-- 第2行 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center"
         >

        <ImageView
            android:id="@+id/img8"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:src="@drawable/img7" />

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            />

        <ImageView
            android:id="@+id/img4"
           android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:src="@drawable/img3" />
    </LinearLayout>
    <!-- 第3行 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center"
        >

        <ImageView
            android:id="@+id/img5"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:src="@drawable/img6" />

        <ImageView
            android:id="@+id/img6"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:src="@drawable/img5" />

        <ImageView
            android:id="@+id/img7"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:src="@drawable/img4" />
    </LinearLayout>
    <!-- 第4行 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="赌博有风险,下注请谨慎"
            
            android:textColor="#f00"
            android:textSize="15sp" />
    </LinearLayout>
    <!-- 第5行 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal"
        android:weightSum="3" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="#f00"
            android:onClick="btn1"
            android:text="下注" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="#0f0"
            android:onClick="btn2"
            android:text="开始" />
    </LinearLayout>
    <!-- 第6行 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:background="#ffff33"
            android:gravity="center"
            android:text="金币:10000" />
    </LinearLayout>

</LinearLayout>

activity_second.xml

<LinearLayout 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:orientation="vertical"
    android:weightSum="10"
    tools:context="com.example.tigerdemo3.SecondActivity" >

    <!-- listview radiobutton button -->

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:listSelector="#f00" />

    <RadioGroup
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4" >

        <RadioButton
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/radiobutton"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="1000" />

        <RadioButton
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/radiobutton"
            android:button="@null"
            android:gravity="center"
            android:text="500" />

        <RadioButton
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/radiobutton"
            android:button="@null"
            android:gravity="center"
            android:text="200" />

        <RadioButton
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/radiobutton"
            android:button="@null"
            android:gravity="center"
            android:text="100" />
    </RadioGroup>

    <Button
        android:id="@+id/btncomfirm"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:text="确定" 
        android:onClick="btnConfirm"/>

</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 作Secondactivity.java的Layout的布局资源 -->

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/img0" />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imageview"
        android:text="甄姬"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/imageview"
        android:layout_toRightOf="@id/imageview"
        android:text="5.0"
        android:textSize="25sp" />

</RelativeLayout>

3.res/drawable
color_checked.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 用于第二个xml的radiobutton按下去的颜色 -->
<color xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#00f" >

</color>

color_unchecked.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 用于第二个xml的radiobutton未按下去的颜色 -->
<color xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ff0" >

</color>

radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 用于第二个xml的radiobutton的颜色 ,资源引用 -->
    <item android:drawable="@drawable/color_checked" android:state_checked="true"/>
    <item android:drawable="@drawable/color_unchecked" android:state_checked="false"/>

</selector>

shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 用于第一个xml边框循环 -->
    <!-- 分别为内边框 、填充、描边、圆角 -->
    <padding
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp"
        android:top="8dp" />

    <solid android:color="#0f0" />

    <stroke
        android:width="2dp"
        android:color="#f00" />

    <corners android:radius="8dp" />

</shape>

这里写图片描述

图片资源

这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐