这篇应该昨天发的,但网络出了点小问题,刚弄上来
想复习一下多线程的相关知识,准备顺便写个小项目,就写个打地鼠吧。
项目需求很简单,就是通常意义上的打地鼠。采用迭代开发的方式,即先完成简单的版本,然后再往上加。还没写完,先把今天的整理一下吧。这个是0.3版本。0.1版本是一些功能的简单测试。0.2版本是主XML的设计。0.3版本加上了响应,只是一个响应的测试,并没有完全的加上,因为整体消息传递逻辑还没设计(就因为没设计,写0.4版本的时候就遇到了不小的问题
ActivityMain.java
package com.fsy;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
/**
* 主Activity
* @author fsy
*
*/
public class ActivityMain extends Activity {
/** Called when the activity is first created. */
ImageButton b[];
Manage manage;
MyClickListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listener=new MyClickListener();
b=new ImageButton[9]; //初始化九个ImageButton
b[0]=(ImageButton)findViewById(R.id.image1);
b[1]=(ImageButton)findViewById(R.id.image2);
b[2]=(ImageButton)findViewById(R.id.image3);
b[3]=(ImageButton)findViewById(R.id.image4);
b[4]=(ImageButton)findViewById(R.id.image5);
b[5]=(ImageButton)findViewById(R.id.image6);
b[6]=(ImageButton)findViewById(R.id.image7);
b[7]=(ImageButton)findViewById(R.id.image8);
b[8]=(ImageButton)findViewById(R.id.image9);
for(int i=0;i<9;i++){
b[i].setOnClickListener(listener);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Manage.status[0]=1; //测试用假设第一个洞口有老鼠出来了
}
}
MyClickListener.java
package com.fsy;
import android.view.View;
import android.view.View.OnClickListener;
/**
*ImageButton的Listener 处理点击后的响应
* @author fsy
*
*/
public class MyClickListener implements OnClickListener{
@Override
public void onClick(View v) {
int status=v.getId();
switch(status){
case R.id.image1 :
if(Manage.status[0] ==1){
v.setBackgroundResource(R.drawable.hitted);
}
break;
}
}
}
Manage.java
package com.fsy;
/**
* 一个管理类,将每个类的对象都传进来,类之间公用数据
* @author fsy
*
*/
public class Manage {
/**
* 记录每个鼠洞的状态,0为没出老鼠,1为出来了老鼠
*/
public static int status[]= new int[9];
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/linearLayout_title"
android:gravity="center_horizontal"
android:layout_height="wrap_content">
<TextView android:text="Score:XXXX"
android:id="@+id/score"
android:textSize="10pt"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/linearLayout_time"
android:layout_height="wrap_content"
>
<ProgressBar android:id="@+id/time_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_marginLeft="5px"
android:layout_marginRight="5px"
android:layout_height="wrap_content"></ProgressBar>
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:id="@+id/tableLayout_mouse"
android:layout_height="wrap_content">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageButton
android:id="@+id/image1"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<ImageButton
android:id="@+id/image2"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<ImageButton
android:id="@+id/image3"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageButton
android:id="@+id/image4"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<ImageButton
android:id="@+id/image5"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<ImageButton
android:id="@+id/image6"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageButton
android:id="@+id/image7"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<ImageButton
android:id="@+id/image8"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<ImageButton
android:id="@+id/image9"
android:background="@drawable/ready"
android:layout_margin="2px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/linearLayout_start"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
>
<Button
android:text="Start"
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
在写0.4版本的时候遇到了这样的一个问题,android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views。我本想在子进程里更改ImageButton的背景,但是看这话的意思,我这想法是不行了。再另想办法吧。另外,数据在各个类之间的传递写的也非常的别扭,今天得好好设计一下~
因篇幅问题不能全部显示,请点此查看更多更全内容