一、实验目的及要求(本实验所涉及并要求掌握的知识点)
实现数独游戏的完整界面设计
掌握ActionBar的使用;
二、实验环境(本实验所使用的硬件设备和相关软件)
(1)PC机
(2)操作系统:Windows XP
(3)软件: Eclipse, JDK1.6,Android SDK,ADT
1) 导入工程Sodoku
2) 添加以下的Activity类
3) 添加以下layout资源文件
4) 完成设计后,界面运行如下
初始界面:
新游戏界面:
帮助界面:
关于界面:
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)
menu菜单代码
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/ai1" android:title="皮肤"> <menu> <item android:id="@+id/ai1_1" android:title="默认(晚霞)"/> <item android:id="@+id/ai1_2" android:title="蓝天"/> <item android:id="@+id/ai1_3" android:title="雨天"/> <item android:id="@+id/ai1_4" android:title="卡通"/> <item android:id="@+id/ai1_5" android:title="星空"/> </menu> </item> <item android:id="@+id/ai2" android:title="@string/action_settings"> </item> <item android:id="@+id/aix" android:showAsAction="always" android:title="新游戏"> </item> <item android:id="@+id/ai3" android:showAsAction="always" android:title="帮助"> </item> <item android:id="@+id/ai4" android:showAsAction="always" android:title="关于"> </item> <item android:id="@+id/ai5" android:actionLayout="@layout/clock" android:showAsAction="always" android:title="时钟"> </item> </menu>
MainActivity关键代码:
actionBar=getActionBar(); actionBar.show(); public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Resources resources; Drawable btnDrawable; item.setChecked(true); switch (item.getItemId()) { case R.id.ai1_1: item.setChecked(true); resources = getBaseContext().getResources(); btnDrawable = resources.getDrawable(R.drawable.ppp); V.setBackground(btnDrawable); break; case R.id.ai1_2: item.setChecked(true); resources = getBaseContext().getResources(); btnDrawable = resources.getDrawable(R.drawable.p2); V.setBackground(btnDrawable); break; case R.id.ai1_3: item.setChecked(true); resources = getBaseContext().getResources(); btnDrawable = resources.getDrawable(R.drawable.p4); V.setBackground(btnDrawable); break; case R.id.ai1_4: item.setChecked(true); resources = getBaseContext().getResources(); btnDrawable = resources.getDrawable(R.drawable.p3); V.setBackground(btnDrawable); break; case R.id.ai1_5: item.setChecked(true); resources = getBaseContext().getResources(); btnDrawable = resources.getDrawable(R.drawable.p5); V.setBackground(btnDrawable); break; case R.id.ai2: item.setChecked(true); Intent intent=new Intent(); intent.setClass(MainActivity.this, music_main.class); startActivity(intent); break; case R.id.ai3: item.setChecked(true); Intent intent1=new Intent(); intent1.setClass(MainActivity.this, help_main.class); startActivity(intent1); break; case R.id.ai4: item.setChecked(true); Intent intent2=new Intent(); intent2.setClass(MainActivity.this, about_main.class); startActivity(intent2); case R.id.aix: item.setChecked(true); Intent intentx=new Intent(); intentx.setClass(MainActivity.this, newgame_main.class); startActivity(intentx); default: break; } return super.onOptionsItemSelected(item); }
关于、帮助、新游戏关键代码:
ActionBar actionBar; actionBar=getActionBar(); actionBar.show(); actionBar.setDisplayShowHomeEnabled(true);//显示应用程序图标 actionBar.setDisplayHomeAsUpEnabled(true); public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub item.setChecked(true); switch (item.getItemId()) { case R.id.ai3: item.setChecked(true); Intent intent1=new Intent(); intent1.setClass(this, help_main.class); startActivity(intent1); finish(); break; case R.id.ai4: item.setChecked(true); Intent intent2=new Intent(); intent2.setClass(this, about_main.class); startActivity(intent2); finish(); break; case android.R.id.home: item.setChecked(true); Intent intenth=new Intent(this,MainActivity.class); //添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 intenth.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Bundle bundle=new Bundle(); bundle.putString("username",""); intenth.putExtras(bundle); startActivity(intenth); finish(); break; case R.id.aix: item.setChecked(true); Intent intentx=new Intent(); intentx.setClass(this, newgame_main.class); startActivity(intentx); finish(); default: break; } return super.onOptionsItemSelected(item); }
运行结果:(截图)
如三已展示
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)
经过实验,熟悉了游戏导航栏设置的基本代码,为游戏增添了方便的操作,是项目更加完整,对于ActionBar的使用,可以应用用于很app,是一个实用的方法。日后还需要提高ActionBar使用的技巧。
menu菜单代码
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/ai1"
android:title="皮肤">
<menu>
<item
android:id="@+id/ai1_1"
android:title="默认(晚霞)"/>
<item
android:id="@+id/ai1_2"
android:title="蓝天"/>
<item
android:id="@+id/ai1_3"
android:title="雨天"/>
<item
android:id="@+id/ai1_4"
android:title="卡通"/>
<item
android:id="@+id/ai1_5"
android:title="星空"/>
</menu>
</item>
<item
android:id="@+id/ai2"
android:title="@string/action_settings">
</item>
<item
android:id="@+id/aix"
android:showAsAction="always"
android:title="新游戏">
</item>
<item
android:id="@+id/ai3"
android:showAsAction="always"
android:title="帮助">
</item>
<item
android:id="@+id/ai4"
android:showAsAction="always"
android:title="关于">
</item>
<item
android:id="@+id/ai5"
android:actionLayout="@layout/clock"
android:showAsAction="always"
android:title="时钟">
</item>
</menu>
MainActivity关键代码:
actionBar=getActionBar();
actionBar.show();
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.main, menu);
returnsuper.onCreateOptionsMenu(menu);
}
publicboolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Resources resources;
Drawable btnDrawable;
item.setChecked(true);
switch (item.getItemId()) {
case R.id.ai1_1:
item.setChecked(true);
resources = getBaseContext().getResources();
btnDrawable = resources.getDrawable(R.drawable.ppp);
V.setBackground(btnDrawable);
break;
case R.id.ai1_2:
item.setChecked(true);
resources = getBaseContext().getResources();
btnDrawable = resources.getDrawable(R.drawable.p2);
V.setBackground(btnDrawable);
break;
case R.id.ai1_3:
item.setChecked(true);
resources = getBaseContext().getResources();
btnDrawable = resources.getDrawable(R.drawable.p4);
V.setBackground(btnDrawable);
break;
case R.id.ai1_4:
item.setChecked(true);
resources = getBaseContext().getResources();
btnDrawable = resources.getDrawable(R.drawable.p3);
V.setBackground(btnDrawable);
break;
case R.id.ai1_5:
item.setChecked(true);
resources = getBaseContext().getResources();
btnDrawable = resources.getDrawable(R.drawable.p5);
V.setBackground(btnDrawable);
break;
case R.id.ai2:
item.setChecked(true);
Intent intent=new Intent();
intent.setClass(MainActivity.this, music_main.class);
startActivity(intent);
break;
case R.id.ai3:
item.setChecked(true);
Intent intent1=new Intent();
intent1.setClass(MainActivity.this, help_main.class);
startActivity(intent1);
break;
case R.id.ai4:
item.setChecked(true);
Intent intent2=new Intent();
intent2.setClass(MainActivity.this, about_main.class);
startActivity(intent2);
case R.id.aix:
item.setChecked(true);
Intent intentx=new Intent();
intentx.setClass(MainActivity.this, newgame_main.class);
startActivity(intentx);
default:
break;
}
returnsuper.onOptionsItemSelected(item);
}
关于、帮助、新游戏关键代码:
ActionBar actionBar;
actionBar=getActionBar();
actionBar.show();
actionBar.setDisplayShowHomeEnabled(true);//显示应用程序图标
actionBar.setDisplayHomeAsUpEnabled(true);
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.main, menu);
returnsuper.onCreateOptionsMenu(menu);
}
publicboolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
item.setChecked(true);
switch (item.getItemId()) {
case R.id.ai3:
item.setChecked(true);
Intent intent1=new Intent();
intent1.setClass(this, help_main.class);
startActivity(intent1);
finish();
break;
case R.id.ai4:
item.setChecked(true);
Intent intent2=new Intent();
intent2.setClass(this, about_main.class);
startActivity(intent2);
finish();
break;
case android.R.id.home:
item.setChecked(true);
Intent intenth=new Intent(this,MainActivity.class);
//添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出
intenth.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle=new Bundle();
bundle.putString("username","");
intenth.putExtras(bundle);
startActivity(intenth);
finish();
break;
case R.id.aix:
item.setChecked(true);
Intent intentx=new Intent();
intentx.setClass(this, newgame_main.class);
startActivity(intentx);
finish();
default:
break;
}
returnsuper.onOptionsItemSelected(item);
}