这是一个比较早做的工程,不完善的,现在把它放上来,暂时只贴源代码,有时间在细细讲解:先给出两个布局代码:crazy_body.xml(主要是用于显示主界面)xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/background"tools:context=".CrazyActivity" >android:id="@+id/rela1"android:layout_width="match_parent"android:layout_height="50dp"android:background="@drawable/bar" >android:id="@+id/button_back"android:layout_width="wrap_content"android:layout_height="40dp"android:layout_alignParentLeft="true"android:layout_marginLeft="7dp"android:layout_marginTop="3dp"android:background="@drawable/share_back_btn" />android:id="@+id/show_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/button_back"android:layout_centerHorizontal="true"android:background="@drawable/show"android:gravity="center"android:text="123"android:textColor="#aabbdd" >android:id="@+id/textView1"android:layout_width="100dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_alignTop="@+id/show_tv"android:layout_marginRight="7dp"android:background="@drawable/bar_money"android:gravity="center"android:text="236"android:textColor="#aabbdd" />android:id="@+id/relativeLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/rela1" >android:id="@+id/button2"android:layout_width="40dp"android:layout_height="50dp"android:layout_marginLeft="7dp"android:layout_marginTop="60dp"android:background="@drawable/delete"android:gravity="bottom|center"android:text="30"android:textColor="#aabbdd"android:textSize="13sp" />android:id="@+id/button3"android:layout_width="40dp"android:layout_height="50dp"android:layout_alignLeft="@+id/button2"android:layout_below="@+id/button2"android:layout_marginTop="17dp"android:background="@drawable/prompt"android:gravity="bottom|center"android:text="120"android:textColor="#aabbdd"android:textSize="13sp" />android:id="@+id/button4"android:layout_width="40dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_alignTop="@+id/button2"android:layout_marginRight="7dp"android:layout_marginTop="42dp"android:background="@drawable/share" />android:id="@+id/imageView2"android:layout_width="200dp"android:layout_height="200dp"android:layout_marginBottom="200dp"android:layout_marginLeft="60dp"android:layout_marginTop="15dp"android:src="@drawable/__00490" />android:id="@+id/linearLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignLeft="@+id/relativeLayout1"android:layout_below="@+id/relativeLayout1"android:layout_marginTop="12dp"android:orientation="vertical" >android:id="@+id/mylinearlayout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:orientation="horizontal" >android:id="@+id/mytablelayout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignLeft="@+id/linearLayout1"android:layout_below="@+id/linearLayout1"android:layout_marginTop="12dp" >win.xml(用于通关后显示):android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/end"android:orientation="vertical" >android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginTop="200dp"android:gravity="center_vertical|center_horizontal"android:text="@string/win"android:textColor="#FF0000"android:textSize="30sp"android:textStyle="bold" />接下来是几个类的java代码:1、CrazyActivity.java用于启动应用,显示主界面和处理用户的回答交互,完成答案的比对,重选,提示和金币充值等业务package com.vekaco.crazyguesspic;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TableLayout;import android.widget.TableRow;import android.widget.TextView;import android.widget.Toast;public class CrazyActivity extends Activity implements OnClickListener {int count = 3;// 用来记忆关卡Button back;// 返回上一题按钮TextView showtv;// 显示当前关卡ImageView iv;// 猜图图片Button[] rightButton;// 用来存储正确的答案的按钮Button[] chooseButton;// 用来存储选项按钮TextView coins;// 积分Button delete;Button prompt;int current = 0;int length;int[] location;String ans = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.crazy_body);back = (Button) this.findViewById(R.id.button_back);showtv = (TextView) this.findViewById(R.id.show_tv);iv = (ImageView) this.findViewById(R.id.imageView2);coins = (TextView) this.findViewById(R.id.textView1);delete = (Button) this.findViewById(R.id.button2);prompt = (Button) this.findViewById(R.id.button3);delete.setOnClickListener(this);prompt.setOnClickListener(this);// 设置当前关卡showtv.setText("" + (count + 1));initPic(count);back.setOnClickListener(this);initLinearLayout();initTableLayout();location = new int[length];Log.i("length", "" + length);}// 改变图片的public void initPic(int count) {int num = Entity.rightPic[count];iv.setImageResource(num);}// 加载正确答案的按钮public void initLinearLayout() {LinearLayout ll = (LinearLayout) findViewById(R.id.mylinearlayout);// 把布局里面的空间清空ll.removeAllViews();ll.removeAllViewsInLayout();// 拿到正确答案的长度length = Entity.rightAnswer[count].length();// 正确答案的按钮,方便以后调用rightButton = new Button[length];for (int i = 0; i < length; i++) {Button b = new Button(this);rightButton[i] = b;// 设置相应的监听事件rightButton[i].setOnClickListener(this);// rightButton[i].setEnabled(false);rightButton[i].setText("");rightButton[i].setEnabled(false);// b.setText(i + "");b.setWidth(40);b.setHeight(40);ll.addView(rightButton[i]);// 把按钮添加到布局中}}// 加载选项的按钮public void initTableLayout() {TableLayout tl = (TableLayout) findViewById(R.id.mytablelayout);tl.setStretchAllColumns(true);tl.removeAllViewsInLayout();tl.removeAllViews();chooseButton = new Button[24];// 一共三行for (int row = 0; row < 3; row++) {TableRow tableRow = new TableRow(this);for (int col = 0; col < 8; col++) {Button btn = new Button(this);// 存储24个按钮,分别放在不同位置chooseButton[row * 8 + col] = btn;// 设置相应的监听事件chooseButton[row * 8 + col].setOnClickListener(this);String s = Entity.chooseAnswer[count].substring(row * 8 + col,row * 8 + col + 1);chooseButton[row * 8 + col].setText(s);// btn.setText(s);tableRow.addView(chooseButton[row * 8 + col]);// 把按钮添加到布局中}// 把tablerow加载到布局中,并指定他的宽和高tl.addView(tableRow, new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));}}public void onClick(View v) {if (v == back) {count--;if (count < 0) {Toast.makeText(CrazyActivity.this, "到顶了亲!", Toast.LENGTH_SHORT).show();count = 0;}// if (v == delete) {//// }// if (v == prompt) {// Log.i("ok", "click");// rightButton[current].setText(Entity.rightAnswer[count]// .substring(current, current + 1));// }else {Log.i("info", "" + count);showtv.setText(count + 1 + "");initPic(count);initLinearLayout();initTableLayout();current = 0;}} else {if (v == delete) {// String[] answer = null;// for (int i = 0; i < Entity.rightAnswer[count].length(); i++)// {// answer[i] = Entity.rightAnswer[count].substring(i, i + 1);//// }// for (int i = 0; i < 24; i++) {// int j;// if (i > Entity.rightAnswer[count].length()) {// j = Entity.rightAnswer[count].length();// } else {// j = i;// }// if (chooseButton[i].getText().toString() != (String)// answer[j]) {// chooseButton[i].setText("");// }// }}if (v == prompt) {Log.i("ok", "click");rightButton[current].setText(Entity.rightAnswer[count].substring(current, current + 1));rightButton[current].setEnabled(true);int flag = 1;for (int row = 0; row < 3; row++) {for (int col = 0; col < 8; col++) {if (chooseButton[row * 8 + col].getText().toString().equals(Entity.rightAnswer[count].substring(current, current + 1))) {chooseButton[row * 8 + col].setText("");chooseButton[row * 8 + col].setEnabled(false);flag = 0;break;}}if (flag == 0) {break;}}if (current == length - 1) {for (int i = 0; i < Entity.rightAnswer[count].length(); i++) {ans += rightButton[i].getText().toString();}}if (ans.equals(Entity.rightAnswer[count])) {if ((count + 1) == Entity.rightPic.length) {Toast.makeText(CrazyActivity.this, "恭喜你已经通关了!",Toast.LENGTH_SHORT).show();Intent intent = new Intent();intent.setClass(CrazyActivity.this, WinActivity.class);startActivity(intent);finish();ans = "";} else {count = count + 1;showtv.setText(count + 1 + "");initPic(count);initLinearLayout();initTableLayout();current = 0;ans = "";int temp = Integer.parseInt(coins.getText().toString());temp += 5;coins.setText("" + temp);}} else {current++;}}for (int row = 0; row < 3; row++) {for (int col = 0; col < 8; col++) {if (v == chooseButton[row * 8 + col]) {if (current < length) {rightButton[current].setEnabled(true);String s = Entity.chooseAnswer[count].substring(row* 8 + col, row * 8 + col + 1);rightButton[current].setText(s);chooseButton[row * 8 + col].setText("");location[current] = row * 8 + col;Log.i("temp", "" + current + "" + location[current]);chooseButton[row * 8 + col].setEnabled(false);if (current == length - 1) {for (int i = 0; i < Entity.rightAnswer[count].length(); i++) {ans += rightButton[i].getText().toString();}}if (ans.equals(Entity.rightAnswer[count])) {if ((count + 1) == Entity.rightPic.length) {Toast.makeText(CrazyActivity.this,"恭喜你已经通关了!", Toast.LENGTH_SHORT).show();Intent intent = new Intent();intent.setClass(CrazyActivity.this,WinActivity.class);startActivity(intent);finish();ans = "";} else {count = count + 1;showtv.setText(count + 1 + "");initPic(count);initLinearLayout();initTableLayout();current = 0;ans = "";int temp = Integer.parseInt(coins.getText().toString());temp += 5;coins.setText("" + temp);}} else {current++;}}if (current == length) {Toast.makeText(CrazyActivity.this,"已经填满了,但是答案不对哦,亲!", Toast.LENGTH_SHORT).show();ans = "";}}}}for (int i = 0; i < length; i++) {if (v == rightButton[i]) {Log.i("location", i + "" + location[i]);int temp_location = location[i];chooseButton[temp_location].setEnabled(true);String temp_text = rightButton[i].getText().toString();chooseButton[temp_location].setText(temp_text);rightButton[i].setEnabled(false);rightButton[i].setText("");current--;ans = "";}}}}}2、Entity.java实体类用于存放资源图片、正确答案和备选答案package com.vekaco.crazyguesspic;public class Entity {// 存储正确答案public static String[] rightAnswer = new String[] { "周杰伦", "黄晓明", "科比","GOOGLE", "霹雳娇娃", "李小龙", "泰坦尼克号" };//public static int[] rightPic = new int[] { R.drawable.__00490,R.drawable.__00551, R.drawable.__00096, R.drawable.__00109,R.drawable.__00006, R.drawable.__00018, R.drawable.__00067};public static String[] chooseAnswer = new String[] {"周杰伦王李四张三陈天翔王李四张三傻不拉王李四张三", "黄晓明王李四张三陈天翔王李四张三傻不拉王李四张三","科比额王李四张三陈天翔王李四张三傻不拉王李四张三", "GOOGLEYAHOBAIDUJAVAANDRO","明王李四霹雳张三陈娇娃天翔王李四张三傻不拉王李四", "黄晓明王李四张三小龙翔王李四张三傻不拉王李四张三","黄晓明王李四张三陈泰王坦李四尼三傻克拉号李四张三" };}3、WinActivity.java用于用户通关成功后启动交互,告知用户已成功完成游戏!package com.vekaco.crazyguesspic;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;public class WinActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.win);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);}}有时间再具体注视各行重要代码的作用。

源代码下载地址:http://down.51cto.com/5719673

更多相关文章

  1. android按钮按下的效果
  2. Android在Button按钮上同时显示文字和图片
  3. Android(Java):按钮复选框点中效果
  4. Android——ImageButton【图片按钮】的点击事件与属性
  5. Android调用拨号按钮CALL_BUTTON
  6. Android设置一个按钮右对齐
  7. Android Studio按钮响应事件(三)
  8. android设置一个通用的控件,比如返回按钮
  9. android 使用xml selector设置按钮点击效果图片

随机推荐

  1. 谷歌Android手机应用开发环境的搭建
  2. Android源代码下载指南(图解)
  3. Android粉丝眼中iOS 7与Android(安卓)4.2
  4. Android(安卓)录音实现追踪(Android(安卓)
  5. Android(安卓)修改spinner 字体颜色 样式
  6. Android的线程和线程池
  7. Android的Socket通信编程实例
  8. Android(安卓)开发艺术探索笔记之三 -- V
  9. 抓包工具Fidder详解(主要来抓取Android中
  10. android消息机制