安卓开发-老虎机案例

安卓开发-老虎机案例,第1张

概述老虎机效果图1.src/MainActivity.javapackagecom.example.tigerdemo3;importjava.util.Random;importandroid.app.Activity;importandroid.content.Intent;importandroid.content.IntentSender.SendIntentException;importandroid.graphics.Color;import

老虎机效果图

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>

图片资源








总结

以上是内存溢出为你收集整理的安卓开发-老虎机案例全部内容,希望文章能够帮你解决安卓开发-老虎机案例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105010.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存