课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016年4月8日 |
实验项目名称 |
数独游戏界面设计 |
实验地点 |
S3010 |
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
|
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||
【目的】 实现数独游戏的完整界面设计 【要求】 掌握ActionBar的使用; |
|||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||
三、实验内容及步骤 |
|||
1) 导入工程Sodoku 2) 添加以下的Activity类
3) 添加以下layout资源文件
|
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)
- MainActivity.java
package com.example.sukodu; import android.annotation.TargetApi; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; @TargetApi(Build.VERSION_CODES.HONEYCOMB) public class MainActivity extends Activity { private Button exitbtn, aboutbtn; private TextView tv1; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getActionBar(); actionBar.show(); ActionBar actionBar1 = getActionBar();// 获取ActionBar对象 actionBar1.setDisplayShowHomeEnabled(true);// 显示应用程序图标 actionBar1.setDisplayHomeAsUpEnabled(true);// 将应用程序图标转变为可点击图标,并添加一个返回箭头。 tv1 = (TextView) findViewById(R.id.textView1); Log.i("test", "333333"); final Intent intent = getIntent(); // 获取Intent对象 Bundle bundle = intent.getExtras(); // 获取传递的数据包 tv1.setText("你的名字是:" + bundle.getString("userName")); aboutbtn = (Button) findViewById(R.id.about_btn); exitbtn = (Button) findViewById(R.id.exit_btn); exitbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // startActivity(intent); setResult(0x1717, intent); Log.i("test", "444444"); finish(); } }); aboutbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent); //finish(); } }); } public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { /* * case android.R.id.home: // 创建启动MainActivity的Intent Intent intent = * new Intent(this, MainActivity.class); // * 添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); * startActivity(intent); break; */ case R.id.help: Intent intent1 = new Intent(this, Help.class); startActivity(intent1); // finish(); break; case R.id.about: Intent intent2 = new Intent(this, About.class); startActivity(intent2); // finish(); break; case R.id.newgame: Intent intent3 = new Intent(this, New_Game.class); startActivity(intent3); // finish(); break; default: break; } return super.onOptionsItemSelected(item); } /* * @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO * Auto-generated method stub getMenuInflater().inflate(R.menu.main, menu); * return true; } * * @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO * Auto-generated method stub if (item.getItemId()==R.id.set) { Intent * intent=new Intent(); * intent.setClass(MainActivity.this,ShuduSettings.class); * startActivity(intent); finish(); * * } return true; } */ }
2.About.java
package com.example.sukodu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; public class About extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.about_layout); } public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { /* * case android.R.id.home: // 创建启动MainActivity的Intent Intent intent = * new Intent(this, MainActivity.class); // * 添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); * startActivity(intent); break; */ case R.id.help: Intent intent1 = new Intent(this, Help.class); startActivity(intent1); // finish(); break; case R.id.about: Intent intent2 = new Intent(this, About.class); startActivity(intent2); // finish(); break; case R.id.newgame: Intent intent3 = new Intent(this, New_Game.class); startActivity(intent3); // finish(); break; default: break; } return super.onOptionsItemSelected(item); } }
3.Help.java
package com.example.sukodu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; public class Help extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.help_layout); } public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { /* * case android.R.id.home: // 创建启动MainActivity的Intent Intent intent = * new Intent(this, MainActivity.class); // * 添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); * startActivity(intent); break; */ case R.id.help: Intent intent1 = new Intent(this, Help.class); startActivity(intent1); // finish(); break; case R.id.about: Intent intent2 = new Intent(this, About.class); startActivity(intent2); // finish(); break; case R.id.newgame: Intent intent3 = new Intent(this, New_Game.class); startActivity(intent3); // finish(); break; default: break; } return super.onOptionsItemSelected(item); } }
4.New_Game.java
package com.example.sukodu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; public class New_Game extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.newgame_layout); } public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { /* * case android.R.id.home: // 创建启动MainActivity的Intent Intent intent = * new Intent(this, MainActivity.class); // * 添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); * startActivity(intent); break; */ case R.id.help: Intent intent1 = new Intent(this, Help.class); startActivity(intent1); // finish(); break; case R.id.about: Intent intent2 = new Intent(this, About.class); startActivity(intent2); // finish(); break; case R.id.newgame: Intent intent3 = new Intent(this, New_Game.class); startActivity(intent3); // finish(); break; default: break; } return super.onOptionsItemSelected(item); } }
5.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sukodu" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sukodu.MainActivity" android:label="@string/shudu" > </activity> <activity android:name="com.example.sukodu.ShuduSettings" android:label="@string/shezhi" > </activity> <activity android:name="com.example.sukodu.LoginActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.sukodu.SecondActivity" android:label="@string/app_name" > </activity> <activity android:name="com.example.sukodu.About" android:label="@string/shudu" > </activity> <activity android:name="com.example.sukodu.Help" android:label="@string/shudu" > </activity> <activity android:name="com.example.sukodu.New_Game" android:label="@string/shudu" > </activity> </application> </manife
1) 完成设计后,界面运行如下 初始界面: 新游戏界面:(注:此界面只是背景,可以自由更换,下同)
帮助界面: 关于界面: |
||
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
||
在这次实验中,自学了老师的参考文档,然后理解后尝试应用在数独游戏界面上,出现了一些语法错误,但通过修改代码或者导入包改正了, 还有一些逻辑上的错误,比较难以发现,所以耗时相对更多些。由于达不到自己理想的效果,通过改代码以及不断运行,最终实现了总体要求 达到的效果。还有一个问题就是版本过低,导致运行不了ActionBar功能,程序崩溃了,但是导入了Android ADK Version18的工具,修改 了Manifest.xml文件的minSDK Version以及Target SDK Version后完美的解决了这个问题。当然,以后还需不断加强Android学习,改善这代码。 |
||
实验评语 |
|
|
实验成绩 |
|
指导教师签名: 年 月 日 |