安卓開發-老虎機案例

老虎機效果圖
這裏寫圖片描述

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>

這裏寫圖片描述

圖片資源
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章