Android基础教程 Sudoku(一)学习笔记

  本文摘自Android基础教程一书里面的Sudoku小程序。制作一个游戏首先要创建一个启动界面。

一、Sudoku

二、界面

  制作一款游戏,当然要有一个良好的界面入口。

  

\res\values\string.xml资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name"> Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="new_game_label">新游戏</string>
<string name="continue_label">继续游戏</string>
<string name="about_label">关于</string>
<string name="exit_label">退出</string>
<string name="about_text">\
数独是一种源自18世纪末的瑞士数学家莱昂哈德·欧拉所创造的拉丁方阵游戏。
在9×9格的大九宫格中有9个3×3格的小九宫格,并提供一定数量的数字。
根据这些数字,利用逻辑和推理,在其它的空格上填入1到9的数字。
每个数字在每个小九宫格内只能出现一次,每个数字在每行、每列也只能出现一次。
这种游戏只需要逻辑思维能力,与数字运算无关。
</string>
  <string name="about_title">About Android Sudoku</string>
</resources>

\res\layout\main.xml

\res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:orientation
="horizontal"
android:padding
="30dip" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:layout_gravity
="center"
android:orientation
="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center"
android:layout_marginBottom
="25dip"
android:text
="@string/main_title"
android:textSize
="24.5sp" />

<Button
android:id="@+id/new_button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/new_game_label" />

<Button
android:id="@+id/continue_button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/continue_label" />

<Button
android:id="@+id/about_button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/about_label" />

<Button
android:id="@+id/exit_button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/exit_label" />
</LinearLayout>

</LinearLayout>

但是上面这个布局在横屏的时候,显示比较难看,为了横屏的时候能够正常显示所有按钮。我们可以新建一个横屏时候的界面布局。在res文件夹下新建一个layout-land文件夹,里面在新建一个一样名称的main.xml文件这样,这样在横屏的时候会读取这个资源文件,作为界面显示。(横屏的快捷键是Ctrl+F11)

\res\layout-land\main.xml

\res\layout-land\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:padding
="15dip"
android:orientation
="horizontal"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height
="fill_parent"
android:orientation
="vertical"
android:paddingRight
="20dip"
android:paddingLeft
="20dip"
android:layout_gravity
="center">
>
<TextView
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/main_title"
android:textSize
="24.5sp"
android:layout_gravity
="center"
android:layout_marginBottom
="25dip"

/>

<TableLayout
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center"
android:stretchColumns
="*"
>
<TableRow >
<Button
android:id="@+id/new_button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/new_game_label"
/>
<Button
android:id="@+id/continue_button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/continue_label"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/about_button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/about_label"
/>
<Button
android:id="@+id/exit_button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/exit_label"
/>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>

三、关于按钮的点击弹出文本框

可以新建一个Activity类并启动它。

关于弹出框的布局文件。

res/layout/about.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="match_parent"
android:layout_height
="match_parent" >

<TextView
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/about_text"
android:textSize
="20sp"
/>

</ScrollView>

新建一个类About继承自Activity

public class About extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}

}

修改Sudoku.java文件让其实现OnClickListener接口,至于为什么不用匿名内部类来处理单击时间,因为根据Android开发人员的经验,每个新的内部类将多占用1kb的内存

Sudoku.java
public class Sudoku extends Activity implements OnClickListener {
protected static final String TAG = "Sudoku";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button about_button = (Button)findViewById(R.id.about_button);
final Button new_button = (Button)findViewById(R.id.new_button);
final Button continue_button = (Button)findViewById(R.id.continue_button);
final Button exit_button = (Button)findViewById(R.id.exit_button);
about_button.setOnClickListener(this);
new_button.setOnClickListener(this);
continue_button.setOnClickListener(this);
exit_button.setOnClickListener(this);
}

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.about_button :
Intent i = new Intent(this,About.class);
startActivity(i);
break;
}
}
}

最后要记得在AndroidManifes.xml文件中声明这个About Activity

在application标签下添加

<activity android:name=".About"

  android:label = "@string/about_title">

</activity>

还可以为activity添加应用主题

<activity android:name=".About"

  android:label = "@string/about_title"

  android:theme="@android:style/Hteme.Dialog"

>

</activity>

点击关于按钮后运行结果如图



posted @ 2011-12-11 14:17  妙慧  阅读(433)  评论(0编辑  收藏  举报