课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016年4月8日 |
||
实验项目名称 |
设计一个简单登陆界面 |
实验地点 |
S3010 |
||
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
4 |
||
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||||
1目的:实现数独游戏的完整界面设计 2要求:掌握ActionBar的使用 |
|||||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||||
三、实验内容及步骤 |
|||||
1) 导入工程Sodoku 2) 添加以下的Activity类 3) 添加以下layout资源文件 4) 完成设计后,界面运行如下
|
|||||
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图) |
代码:
menu/actionbar
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <group android:id="@+id/group1"> <item android:id="@+id/newgame" android:title="新游戏" android:showAsAction="always"> </item> <item android:id="@+id/help" android:title="帮助" android:showAsAction="always"> </item> <item android:id="@+id/about" android:title="关于" android:showAsAction="always"> </item> <item android:id="@+id/clock" android:title="时钟" android:actionLayout="@layout/clock" android:showAsAction="always"> </item> </group> </menu>
menu/main
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/app_name"/> <item android:id="@+id/item1" android:alphabeticShortcut="B" android:orderInCategory="200" android:title="返回"> </item> <item android:id="@+id/item2" android:title="背景颜色"> </item> <item android:id="@+id/item3" android:title="前景颜色"> <menu> <item android:id="@+id/itemRed" android:title="红色"/> <item android:id="@+id/itemGreen" android:title="绿色"/> <item android:id="@+id/itemBlue" android:title="蓝色"/> </menu> </item> </menu>
AboutActivity
package com.example.sukodu; import org.apache.http.impl.conn.DefaultClientConnection; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class AboutActivity extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.about); ActionBar abar =getActionBar(); abar.setDisplayShowHomeEnabled(true); abar.setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.actionbar, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Intent intent=getIntent(); switch(item.getItemId()){ case android.R.id.home: // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // startActivity(intent); finish(); break; case R.id.help : intent.setClass(this, HelpActivity.class); startActivity(intent); finish(); break; case R.id.newgame : intent.setClass(this, GamesActivity.class); startActivity(intent); finish(); break; default :{ break; } } return super.onOptionsItemSelected(item); } }
GamesActivity
package com.example.sukodu; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class GamesActivity extends Activity{ @SuppressLint("NewApi") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.games); ActionBar abar =getActionBar(); abar.setDisplayShowHomeEnabled(true); abar.setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.actionbar, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Intent intent = getIntent(); switch(item.getItemId()){ case android.R.id.home:{ finish(); break; } case R.id.about: { intent.setClass(this, AboutActivity.class); startActivity(intent); finish(); break; } case R.id.help :{ intent.setClass(this, HelpActivity.class); startActivity(intent); finish(); break; } default:{ break; } } return super.onOptionsItemSelected(item); } }
HelpActivity
package com.example.sukodu; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class HelpActivity extends Activity { @SuppressLint("NewApi") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.help); ActionBar abar = getActionBar(); abar.setDisplayShowHomeEnabled(true); abar.setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.actionbar, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Intent intent = getIntent(); switch (item.getItemId()) { case android.R.id.home: { finish(); break; } case R.id.about: { intent.setClass(this, AboutActivity.class); startActivity(intent); finish(); break; } case R.id.newgame :{ intent.setClass(this, GamesActivity.class); startActivity(intent); finish(); break; } default:{ break; } } return super.onOptionsItemSelected(item); } }
MainActivity
package com.example.sukodu; import android.app.Activity; import android.content.Intent; import android.content.res.ColorStateList; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button btabout, btnewgame; Intent intent; private Button exitbtn, aboutbtn; private TextView tv1, titleview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.textView1); titleview = (TextView) findViewById(R.id.mainTitle); btnewgame=(Button) findViewById(R.id.new_btn); Log.i("test", "333333"); final Intent intent = getIntent(); // 获取Intent对象 Bundle bundle = intent.getExtras(); // 获取传递的数据包 tv1.setText("你的名字是:" + bundle.getString("userName")); btnewgame.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent it = new Intent(); it.setClass(MainActivity.this, GamesActivity.class); startActivity(it); } }); 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, AboutActivity.class); startActivity(intent); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); getMenuInflater().inflate(R.menu.actionbar, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Intent it = new Intent(); switch (item.getItemId()) { case R.id.itemRed: titleview.setTextColor(Color.RED); break; case R.id.itemGreen: titleview.setTextColor(Color.GREEN); break; case R.id.help: { it.setClass(this, HelpActivity.class); startActivity(it); break; } case R.id.about: { it.setClass(this, AboutActivity.class); startActivity(it); break; } case R.id.newgame: { it.setClass(this, GamesActivity.class); startActivity(it); break; } default:{ break; } } return super.onOptionsItemSelected(item); } }
运行截图