这是一个比较早做的工程,不完善的,现在把它放上来,暂时只贴源代码,有时间在细细讲解: 先给出两个布局代码: 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 = ""; @Override protected 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 { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.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警告——Buttons in button bars should be border
  2. Android(安卓)圆角的Button
  3. android listview adater
  4. android按钮按下的效果
  5. Android(安卓)计算器
  6. Android(安卓)自动更新代码
  7. Android在Button按钮上同时显示文字和图片
  8. Android(Java):按钮复选框点中效果
  9. android音乐播放器常见操作

随机推荐

  1. Android Notifications通知
  2. Android About AndroidManifest.xml
  3. android 常用组建案例
  4. android viewPage 判断是否到最后一页
  5. Android 的 Button 按钮实现的两种方式
  6. Android MAC配置adb
  7. Android Contacts的使用(二)
  8. Android超炫日期日历控件:TimesSquare
  9. Android不同层次开启硬件加速的方式
  10. Android官方ORM Room介绍