这个游戏的功能类似打地鼠。

项目地址: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];@Overrideprotected 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;}@Overridepublic 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"><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:text="当前分数:0"android:layout_weight="1" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" android:layout_weight="2"android:orientation="horizontal"><Buttonandroid:id="@+id/button0"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#EEEEEE"/><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#EEEEEE"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#EEEEEE"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" android:layout_weight="2"android:orientation="horizontal"><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="left"android:layout_weight="1"android:background="#EEEEEE"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#EEEEEE"/><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="right"android:layout_weight="1"android:background="#EEEEEE"/></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content" android:layout_weight="2"android:gravity="bottom"><Buttonandroid:id="@+id/button6"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#EEEEEE"/><Buttonandroid:id="@+id/button7"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#EEEEEE"/><Buttonandroid: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

效果:

转载于:https://www.cnblogs.com/moonlightpoet/p/5405796.html

Android 使用线性布局LinearLayout和Button实现一个点红块游戏相关推荐

  1. Android Studio 线性布局Linearlayout的控件位置控制l属性Layout_margin失效问题解决

    Android Studio 线性布局Linearlayout的控件位置控制l属性Layout_margin失效问题解决 问题:如layout_marginRight ="50dp" ...

  2. Android学习笔记(11):线性布局LinearLayout

    线性布局LinearLayout是指在横向或是竖向一个接一个地排列.当排列的组件超出屏幕后,超出的组件将不会再显示出来. LinearLayout支持的XML属性和相应方法如表所看到的: Attrib ...

  3. Android布局之线性布局LinearLayout(二) ----简单模仿ios端小米计算器主界面UI

    Android布局之线性布局LinearLayout(二) ----简单模仿ios端小米计算器主界面UI   今天老师的要求是让用LinearLayout布局做自己手机自带的计算器的UI设计,因为io ...

  4. 【Android】线性布局(LinearLayout)最全解析

    [Android]线性布局(LinearLayout)最全解析 一.LinearLayout概述 二.LinearLayout常用属性 2.1 orientation属性 2.2 gravity属性 ...

  5. Android 应用开发(第3章)线性布局LinearLayout

    1.线性布局(LinearLayout) 线性布局是Android中较为常用的布局方式,它使用<LinearLayout>标签.LinearLayout 线性布局有两种,分别是水平线性布局 ...

  6. 线性布局LinearLayout

    线性布局LinearLayout 一.简介 LinearLayout是一种线型的布局方式.LinearLayout布局容器内的组件一个挨着一个地排列起来:不仅可以控制个组件横向排列,也可控制各组件纵向 ...

  7. Android Studio:线性布局

    目录 一.导入新课 二.新课讲解 (一)界面与布局 1.界面 2.布局 (1)视图层次结构图 (3)UI控件 (Control) (4)两种方式声明布局 (二)线性布局 1.继承关系图 2.常用属性 ...

  8. Android:安卓线性布局(属性)

    安卓线性布局(属性) 安卓六大布局: AbsoluteLayout  绝对布局(已过期,不建议使用) RelativeLayout    相对布局 LinearLayout       线性布局 Fr ...

  9. Andriod Studio 线性布局(LinearLayout)

    Andriod有六种基本布局:线性布局LinearLayout.相对布局RelativeLayout.表格布局TableLayout.层布局FrameLayout.绝对布局AbsoluteLayout ...

最新文章

  1. centos7下没有iptables
  2. zcmu-1957(状态压缩枚举)
  3. Engagement Center launchpad点了Tile后的处理机制
  4. 王爽 汇编语言第三版 第9章 转移指令的原理
  5. 注册页面的JSON响应方式详细分析(与前端页面交互方式之一)
  6. 史无前例,阿里云或将空降 M7 级高管
  7. 01.MVC5安装Ext.Net
  8. PowerShell 以管理员身份运行 cmd(命令行窗口),或其他程序
  9. ArcGIS 起伏度、坡度、交通便利度数据生成
  10. 收评:5月24日资金流向(摘自益盟)
  11. DNS解析域名解析过程
  12. u盘装系统学计算机好,新手想学重装系统难不难?详细的教程来了,学不会只能怪自己了...
  13. mysql添加表或字段注释
  14. 【研0需要知道的那些事01】如何判断期刊是否为核心期刊,知网导出参考文献越来越多怎么办?
  15. ModuleNotFoundError: No module named ‘pandas.tseries‘
  16. Ubuntu 22.04 双系统安装和卸载
  17. Processing入门教程
  18. 华为机试:最长方连续方波信号
  19. 程序员接私活的那些事
  20. VIPLE 对变量实现一个加法计算

热门文章

  1. 【Qt】QModbusRtuSerialSlave类
  2. 【嵌入式】Libmodbus之RTU模式Master端程序示例
  3. 【Linux】一步一步学Linux——readonly命令(219)
  4. 【Linux】一步一步学Linux——users命令(101)
  5. 逆向入门--简单win32CK逆向手记
  6. 每天一道LeetCode-----计算二叉树所有根节点到叶子节点的和
  7. 毕业设计(论文)-c++小型ftp服务器系统【毕业论文】.doc,毕业设计(论文)-C++小型FTP服务器系统.doc...
  8. MFC使用CCommandLineInfo处理命令行
  9. imageloader 的 java.security.cert.CertPathValidatorException
  10. Problem - 6111迷宫出逃