实验五
实验报告
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016年4月14日 |
实验项目名称 |
数独游戏界面设计 |
实验地点 |
S3010 |
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
2 |
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||
|
|||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||
三、实验内容及步骤 |
|||
1) 使用ActionBar显示OptionMenu的菜单项MenuItem; 2) 使用程序图标导航; 3) 添加Action View; 4) 导入工程Sodoku ; 5) 添加Activity类; 6) 添加layout资源文件。 |
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)
AboutActivity activity:
package com.example.sudoku; import com.example.sudoku.R.color; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.TextView; public class AboutActivity extends Activity { TextView tv; protected void onCreate(Bundle savedInstanceState) { ActionBar actionBar=getActionBar(); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); super.onCreate(savedInstanceState); setContentView(R.layout.about); tv=(TextView)findViewById(R.id.textView1); } public boolean onCreateOptionsMenu(Menu menu) { 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: this.finish(); break; case R.id.item2: Intent intent1=new Intent(this,Help.class); startActivity(intent1); this.finish(); break; case R.id.item3: Intent intent2=new Intent(this,AboutActivity.class); startActivity(intent2); this.finish(); break; case R.id.item1: Intent intent3=new Intent(this,GameActivity.class); startActivity(intent3); this.finish(); break; default: break; } return super.onOptionsItemSelected(item); } }
Help activity:
package com.example.sudoku; import com.example.sudoku.R.color; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.TextView; public class Help extends Activity { TextView tv; protected void onCreate(Bundle savedInstanceState) { ActionBar actionBar=getActionBar(); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); super.onCreate(savedInstanceState); setContentView(R.layout.help); tv=(TextView)findViewById(R.id.textView1); } 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: this.finish(); break; case R.id.item2: Intent intent1=new Intent(this,Help.class); startActivity(intent1); this.finish(); break; case R.id.item3: Intent intent2=new Intent(this,AboutActivity.class); startActivity(intent2); this.finish(); break; case R.id.item1: Intent intent3=new Intent(this,GameActivity.class); startActivity(intent3); this.finish(); break; default: break; } return super.onOptionsItemSelected(item); } }
MainActivity activity:
package com.example.sudoku; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.drm.DrmStore.Action; 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; public class MainActivity extends Activity { TextView aa; Button btn,btn1,btn2; private Bundle bunble; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.about_button); btn1=(Button)findViewById(R.id.butt1); btn2=(Button)findViewById(R.id.butt2); Intent intent=getIntent(); Bundle bundle=intent.getExtras(); String str=bundle.getString("str"); aa=(TextView)findViewById(R.id.haha); aa.setText(str); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent1=new Intent(); intent1.setAction(Intent.ACTION_CALL); intent1.setData(Uri.parse("tel:10086")); startActivity(intent1); } }); btn2.setOnClickListener(new OnClickListener() { // private String msn; @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent2=new Intent(); intent2.setAction(Intent.ACTION_SENDTO); // intent2.putExtra("sms_body", "gfdgfh"); intent2.setData(Uri.parse("smsto:13500135000")); startActivity(intent2); } }); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(); // intent.setClass(MainActivity.this, AboutActivity.class); intent.setAction("android.intent.action.About"); startActivity(intent); } }); } @Override 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 R.id.item2: Intent intent1=new Intent(this,Help.class); startActivity(intent1); break; case R.id.item3: Intent intent2=new Intent(this,AboutActivity.class); startActivity(intent2); break; case R.id.item1: Intent intent3=new Intent(this,GameActivity.class); startActivity(intent3); break; default: break; } return super.onOptionsItemSelected(item); } }
GameActivity activity:
package com.example.sudoku; import com.example.sudoku.R.color; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; 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; import android.widget.Toast; public class GameActivity extends Activity { TextView tv; Button btnStart; Button btnPause; Button btnstop; public MediaPlayer mediaPlayer; protected void onCreate(Bundle savedInstanceState) { mediaPlayer=new MediaPlayer(); mediaPlayer=MediaPlayer.create(GameActivity.this, R.raw.nan); ActionBar actionBar=getActionBar(); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); super.onCreate(savedInstanceState); setContentView(R.layout.game); tv=(TextView)findViewById(R.id.textView1); btnStart=(Button)findViewById(R.id.button1); btnPause=(Button)findViewById(R.id.button2); btnstop=(Button)findViewById(R.id.button3); btnstop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub mediaPlayer.stop(); } }); btnPause.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(mediaPlayer.isPlaying()){ mediaPlayer.pause(); // Toast.makeText(GameActivity.this, "hhhhhhhhhhhh", Toast.LENGTH_LONG); btnPause.setText("continue"); } } }); btnStart.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub mediaPlayer.start(); } }); } 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: this.finish(); break; case R.id.item2: Intent intent1=new Intent(this,Help.class); startActivity(intent1); this.finish(); break; case R.id.item3: Intent intent2=new Intent(this,AboutActivity.class); startActivity(intent2); this.finish(); break; case R.id.item1: Intent intent3=new Intent(this,GameActivity.class); startActivity(intent3); this.finish(); break; default: break; } return super.onOptionsItemSelected(item); } }
about layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="helloworld" android:textSize="50dp"/> </LinearLayout>
clock:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <AnalogClock android:id="@+id/analogClock1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
help layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="帮助" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout>
game layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暂停" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" /> </LinearLayout> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/bkgrnd" /> </LinearLayout>