现在的位置: 首页 > 综合 > 正文

Android的数独游戏

2012年11月17日 ⁄ 综合 ⁄ 共 2193字 ⁄ 字号 评论关闭

刚开始接触android,想写弄个小玩意出来红练习一下,刚好有一份独数游戏的教程,就先以这个小玩意练刀了。

 

先看下模拟器里的效果吧:

安装环境也是费了一些时间,从来未用过Java, 所以安装过程我也记录在了另一篇文章里。第一次遇到的问题就是要写界面,Android的界面用的是XML文件的标识的, 大家都知道XML文件的效率其实并不高的,呵呵Google这样神一样的公司当然也知道了,其实 Google是把XML文件压缩成二进制的放进apk里的,XML只是用来在开发的时候用,所以效率就不是问题了。 顺便介绍一个工具droiddraw画界面比eclipse里自带的那个要好很多。

按钮的响应:

要在onCreate文件里添加:

   1:   View aboutButton = this.findViewById(R.id.btn_about);
   2:    aboutButton.setOnClickListener((OnClickListener) this);

然后写onClick方法:

   1: public void onClick(View v) 

   2:     {

   3:         // TODO Auto-generated method stub

   4:         switch(v.getId())

   5:         {

   6:         case R.id.btn_about:

   7:             Intent iAbout = new Intent(this, About.class);

   8:             startActivity(iAbout);

   9:             break;

  10:         case R.id.btn_exit:

  11:             finish();

  12:             break;

  13:         case R.id.btn_newgame:

  14:             openNewGameDialog();

  15:             return;            

  16:         }        

  17:     }    

显示一个界面用了

Intent iAbout = new Intent(this, About.class);
 startActivity(iAbout);
 
关于 Intent 可以看看 Google的Dev Guide里的介绍:

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:

    *

      action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

    *

      data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.

详细可自行查阅…/android/content/Intent.html

 

访问资源:

Android访问资源还是很方便的,只要在res/目录下添加好后就可以在代码里直接访问了,比如添加一个字符串在res/values/strings.xml文件里添加一个<string name="easy_label">简单</string>这样的结点,在代码里用R.id.easy_label就可以得到“简单”这个字符串,够简单吧!

其它的类似。

 

AndroidManifest.xml

又是一个神奇的文件,每一个activity要在这个文件里注册一下,要不然运行的时候会崩溃^_^

<activity android:name=".About"

                 android:label="@string/about_title"

                 android:theme="@android:style/Theme.Dialog">

</activity>

还可以指定界面的Style.

arrays.xml

可以把一个列表放在里面,用R.array.XX  加载后会直接加载成数组,很方便。

 

 

 

游戏的逻辑 就不讲了,到处都是,我关心的是如何在Android上开发。

源码我稍等会弄上来,欢迎大家拍砖、交流^_^!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

抱歉!评论已关闭.