Android 使用线性布局LinearLayout和Button实现一个点红块游戏
这个游戏的功能类似打地鼠。
项目地址:https://github.com/moonlightpoet/RedBlock
程序下载试玩地址:https://github.com/moonlightpoet/RedBlock/blob/master/bin/RedPoint.apk?raw=true
主要代码:
package com.example.redpoint; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build; public class MainActivity extends ActionBarActivity { private int redId;
private int score = 0;
private TextView textView1;
private Button[] buttons = new Button[9]; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText("当前得分:0");
buttons[0] = (Button) findViewById(R.id.button0);
buttons[1] = (Button) findViewById(R.id.button1);
buttons[2] = (Button) findViewById(R.id.button2);
buttons[3] = (Button) findViewById(R.id.button3);
buttons[4] = (Button) findViewById(R.id.button4);
buttons[5] = (Button) findViewById(R.id.button5);
buttons[6] = (Button) findViewById(R.id.button6);
buttons[7] = (Button) findViewById(R.id.button7);
buttons[8] = (Button) findViewById(R.id.button8); redId = (int) (Math.random() * 9) % 9;
buttons[redId].setBackgroundColor(Color.rgb(255, 0, 0)); for (int i = 0; i < 9; i ++) {
buttons[i].setOnClickListener(new MyOnClickListener(this, i));
}
} class MyOnClickListener implements OnClickListener { private Activity context;
private int id; public MyOnClickListener(Activity context, int id) {
this.context = context;
this.id = id;
} @Override
public void onClick(View arg0) {
if (id == redId) {
score += 10;
buttons[redId].setBackgroundColor(Color.rgb(238, 238, 238));
redId = (int) (Math.random() * 9) % 9;
buttons[redId].setBackgroundColor(Color.rgb(255, 0, 0));
} else {
score -= 10;
}
textView1.setText("当前得分:" + score);
} }
}
MainActivity.java
<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"
> <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:text="当前分数:0"
android:layout_weight="1" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal"
> <Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EEEEEE"
/> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EEEEEE"
/> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EEEEEE"
/> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal"
> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left"
android:layout_weight="1"
android:background="#EEEEEE"
/> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EEEEEE"
/> <Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:layout_weight="1"
android:background="#EEEEEE"
/> </LinearLayout> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="bottom"
> <Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EEEEEE"
/> <Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EEEEEE"
/> <Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EEEEEE"
/> </LinearLayout> </LinearLayout>
activity_main.xml
效果:

Android 使用线性布局LinearLayout和Button实现一个点红块游戏的更多相关文章
- Android 自学之线性布局 LinearLayout
线性布局(LinearLayout),线性布局有点想AWT编程里面的FolwLayout,他们都会将容器里面的组件挨个的排列起来. 他们最大的区别在于:Android的线性布局不会换行:AWT里面的F ...
- Android学习笔记(11):线性布局LinearLayout
线性布局LinearLayout是指在横向或是竖向一个接一个地排列.当排列的组件超出屏幕后,超出的组件将不会再显示出来. LinearLayout支持的XML属性和相应方法如表所看到的: Attrib ...
- 线性布局(LinearLayout)
线性布局(LinearLayout) 备注 match_parent填充布局单元内尽可能多的空间 wrap_content完整显示控件内容 orientation有两个值,horizontal水平显示 ...
- Android——布局(线性布局linearLayout,表格布局TableLayout,帧布局FrameLayout)
线性布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...
- Android布局之线性布局——LinearLayout
本文将详细介绍线性布局的各种xml属性. xml属性 <?xml version="1.0" encoding="utf-8"?> <Line ...
- .Net程序猿玩转Android开发---(6)线性布局LinearLayout
LinearLayout控件是Android中重要的布局控件,是一个线性控件,所谓线性控件的意思是指该控件里面的内容仅仅能水平或垂直排列.也就 ...
- 安卓开发06:布局-线性布局 LinearLayout
LinearLayout把视图组织成一行或一列.子视图能被安排成垂直的或水平的.线性布局是非常常用的一种布局方式. 请看一个布局例子: <LinearLayout xmlns:android=& ...
- 线性布局LinearLayout
常用属性 id:控件唯一属性 android:id="@+id/ll_1" --------------------------------------- layout_width ...
- 安卓之线性布局LinearLayout
一.xml属性 (1)orientation:指定线性布局的方向 (2)gravity:指定布局内部视图与本线性布局的对齐方式 (3)layout_weight:指定当前视图的宽或高占上级 ...
随机推荐
- 寻找最大的k个数问题
这是编程之美书第2.5节的一道题目. 各种解法: 解法一,用nlgn复杂度的排序算法对数组进行从大到小排序,取前K个.但这方法做了两件不必要做的事:它对想得到的K个数进行了排序,对不想得到的n-K个数 ...
- 三级级联查询省份名称和编码(保证名称不反复)的SQL语句
三级级联查询省份名称和编码(保证名称不反复)的SQL语句 1.省份.地市和县级数据库表 2.SQL语句 SELECT DISTINCT t.`province_name`,t.`province_co ...
- C语言 · 陶陶摘苹果
算法提高 陶陶摘苹果 时间限制:1.0s 内存限制:256.0MB 问题描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出n个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个3 ...
- C语言 · 求圆面积表面积体积
算法提高 3-3求圆面积表面积体积 时间限制:1.0s 内存限制:256.0MB 问题描述 接受用户输⼊的数值,输出以该值为半径的(1)圆面积,(2)球体表面积,(3)球体体积.pi ...
- RP2833 指示灯说明
ARM-IO9 OUT0 PA8 备用 P5-A4管脚,可以连接74HC164D级联 ARM-IO10 OUT7 PA1 3 ...
- Spring事务处理时自我调用的解决方案 嵌套AOP
开涛的解决方案1 http://jinnianshilongnian.iteye.com/blog/1487235 AopContext.currentProxy() 原理 http://books. ...
- windows系统IIS服务安装
打开控制面板,win8可以使用快捷键win键+X打开列表 打开程序和功能 打开左上角启用或关闭windows功能 打开internet信息服务下拉单 按照下列图中进行对应项勾选 第一个 ...
- cause: java.lang.IllegalStateException: Serialized class com.taotao.pojo.TbItem must implement java.io.Serializable
HTTP Status 500 - Request processing failed; nested exception is com.alibaba.dubbo.rpc.RpcException: ...
- 使用特殊构造的5GB文件测试Win2012Dedup功能
WinServer2012一个崭新的功能就是支持重复数据删除功能.为了测试这个功能的情况,特别构造了一个特殊的5GB文件用于此次测试.惯例,先说下测试环境的搭建1 使用ESXi5上的版本8的VM,安装 ...
- GAN(Generative Adversarial Nets)的发展
GAN(Generative Adversarial Nets),产生式对抗网络 存在问题: 1.无法表示数据分布 2.速度慢 3.resolution太小,大了无语义信息 4.无reference ...