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:指定当前视图的宽或高占上级 ...
随机推荐
- C# Winform获取bin目录的路径
//获取到bin目录的下层路径:bin\Debug\ string aa = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBa ...
- Docker——MacOS上安装运行docker
近几年来,Docker越来越流行,使用场景也越来越广泛.为了能尽快跟上时代步伐.学习并应用到实际工作中,我也开始了Docker之旅. Docker版本 Docker共有两种版本: 社区版(Commun ...
- 欢迎使用markdowm编辑器
欢迎使用Markdown编辑器写博客 [Mark一下CSDN用Markdown的写法而已] 本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown ...
- C语言 · s01串
算法训练 s01串 时间限制:1.0s 内存限制:256.0MB 问题描述 s01串初始为"0" 按以下方式变换 0变1,1变01 输入格式 1个整数(0~19) ...
- Win10技巧:如何确定电脑是否适用Hyper-V虚拟机?
既然微软想要为Hyper-V的普及铺路,那么各种套路……配套措施当然也会一并跟上.比如想要看出电脑是否符合Hyper-V配置要求,有至少两种方式可以参考. 方法一:系统信息 这方法很简单,在Corta ...
- iOS彩票项目--第三天,搭建竞技场和发现,搭建幸运选号和我的彩票界面
一.竞技场搭建--UISegmentedControl的使用 // 重写 自定义控制器的view - (void)loadView { UIImageView *imgView = [[UIImage ...
- arduino~snprintf
#include <stdio.h> printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf ...
- larave框架的安装
(1)中文官网:http://www.golaravel.com/ (2)composer下载与安装 1:composer网址:getcomposer.org 2:windows下载Composer- ...
- Javascript中最常用的61个经典技巧[转]
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu= ...
- Flume1.5.0入门:安装、部署、及flume的案例
转自:http://www.aboutyun.com/thread-8917-1-1.html 问题导读1.什么是flume2.flume的官方网站在哪里?3.flume有哪些术语?4.如何配置flu ...