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:指定当前视图的宽或高占上级 ...
随机推荐
- Makefile 编译动态库文件及链接动态库
本文为原创文章,转载请指明该文链接 文件目录结构如下 dynamiclibapp.c Makefile comm/inc/apue.h comm/errorhandle.c dynamiclib/Ma ...
- libtommath.a: could not read symbols: Bad value 编译错误
最近做个项目需要RSA,便调用了tommath,平时开发环境都在32位的系统上,编译运行一切都没问题,但当把程序换到一台64位系统上编译时出现: /usr/bin/ld: /usr/lib/gcc/x ...
- CMake 使用方法
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的 ...
- Bit operator: Left shift and Right shift (Signed or unsigned? )
No matter left shift or right shift, the result's sign should always be the same as its left operand ...
- 透明遮罩图层VS高斯模糊滤镜 效果分析
前端流行布局中最常见的弹出图层有popup, 对话框, tooltip等, 他们都使用了新的图层,但是实现办法各不相同, 有 的是通过半通明的黑白图层实现的, 有的是通过滤镜实现的, 我们来研究一下两 ...
- 关于SqlServer数据库C盘占用空间太大问题
工程需要用上了SQL SERVER2008 ,主要作为数据仓库使用,使用SSIS包从ORACEL10G中抽取数据到MS SQL中.环境是win2003x64的,驱动使用的oracle10gX64.使用 ...
- Oracle中的job的定时任务
Oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相关视图 select * from dba_jobs; selec ...
- ROS 教程之 navigation :在 catkin 环境下创建costmap layer plugin
在做机器人导航的时候,肯定见到过global_costmap和local_costmap.global_costmap是为了全局路径规划服务的,如从这个房间到那个房间该怎么走.local_costma ...
- WinInet 小例子
#include <windows.h> #include <wininet.h> #include <string> #include <iostream& ...
- PHP 正则表达式 及常用汇总
Δ 定界符 Δ 字符域 Δ 修饰符 Δ 限定符 Δ 脱字符 Δ 通配符(正向预查,反向预查) Δ 反向引用 Δ 惰性匹配 Δ 注释 Δ 零字符宽 1. 平时做网站经常 ...