《Android应用开发》实验报告
实验序号:06 实验项目名称:利用导航图标实现层级式导航 学 号 实验地点 姓 名 指导教师 专业、班 实验时间 一、实验目的及要求 (1)熟悉Eclipse集成环境的开发步骤; (2)了解如何实现层级式导航; (3)掌握如何利用导航图标实现层级式导航。 二、实验设备(环境)及要求 1、winXP虚拟机; 2、Eclipse集成开发环境。 三、实验内容与步骤 (1)修改新建项目的res/layout目录下的布局文件activity_main.xml,将默认添加TextView组件删除,然后添加一个id为next的按钮,设置显示文字为“奋斗”; (2)在MainActivity的onCreat()方法中,获取布局文件中添加的按钮,并为其设置单击事件,实现启动另一个Activity功能; (3)创建详细页面Activity布局文件,名称为activity_detail.xml,在该文件中主要添加一个文本框组件,显示关于奋斗的详细信息; (4)创建DetailActivity,并重写它的onCreate()方法和onCreatOptionsMenu()方法,并且在onCreated()方法中,添加代码控制导航图标的显示; (5)重写onOptionsItemSelected()方法,实现通过调用NavUtils.getParentActivityName()方法;检查元数据是否指定了父Activity,
如果指定,则调用navigateUpFromSameTask()方法导航至副Activity; (6)在AndroidManifest.xml中,使用标记的子标记配置Activity的父Activity (7)运行实例,查看实验结果。 四、程序调试(结果及分析) 五、总结与体会 通过本次实验,知道了如何设计一个层级式导航,并设计导航栏返回按钮,使得可以从子Activity返回到父Activity,安卓开发的能力更强了。六、教师评语 签名: 日期: 附关键代码: 1. Xml代码:2.android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\"android:theme=\"@style/AppTheme.AppBarOverlay\">android:layout_width=\"match_parent\"android:layout_height=\"?attr/actionBarSize\" android:background=\"?attr/colorPrimary\"app:popupTheme=\"@style/AppTheme.PopupOverlay\" /> android:id=\"@+id/next\" android:layout_width=\"100dp\" android:layout_height=\"100dp\" android:layout_marginTop=\"50dp\" android:text=\"关于理想\" />成绩 2. Acivity代码: public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);Button btn=(Button)findViewById(R.id.next);btn.setOnClickListener(new View.OnClickListener() { @Overridepublic void onClick(View v) {Intent intent=new Intent(MainActivity.this,DetailActivity.class);startActivity(intent); } }); }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }return super.onOptionsItemSelected(item); } }public class DetailActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activityl_detail); }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();//noinspection SimplifiableIfStatement if (id == android.R.id.home) {if (NavUtils.getParentActivityName(DetailActivity.this)!=null){ NavUtils.navigateUpFromSameTask(DetailActivity.this); }return true; }else {return super.onOptionsItemSelected(item); } } }
六、教师评语 签名: 日期: 附关键代码: 1. Xml代码:
2.
android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\"android:theme=\"@style/AppTheme.AppBarOverlay\">android:layout_width=\"match_parent\"android:layout_height=\"?attr/actionBarSize\" android:background=\"?attr/colorPrimary\"app:popupTheme=\"@style/AppTheme.PopupOverlay\" /> android:id=\"@+id/next\" android:layout_width=\"100dp\" android:layout_height=\"100dp\" android:layout_marginTop=\"50dp\" android:text=\"关于理想\" />成绩 2. Acivity代码: public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);Button btn=(Button)findViewById(R.id.next);btn.setOnClickListener(new View.OnClickListener() { @Overridepublic void onClick(View v) {Intent intent=new Intent(MainActivity.this,DetailActivity.class);startActivity(intent); } }); }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }return super.onOptionsItemSelected(item); } }public class DetailActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activityl_detail); }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();//noinspection SimplifiableIfStatement if (id == android.R.id.home) {if (NavUtils.getParentActivityName(DetailActivity.this)!=null){ NavUtils.navigateUpFromSameTask(DetailActivity.this); }return true; }else {return super.onOptionsItemSelected(item); } } }
android:theme=\"@style/AppTheme.AppBarOverlay\">
android:layout_width=\"match_parent\"android:layout_height=\"?attr/actionBarSize\" android:background=\"?attr/colorPrimary\"app:popupTheme=\"@style/AppTheme.PopupOverlay\" />
android:layout_height=\"?attr/actionBarSize\" android:background=\"?attr/colorPrimary\"
app:popupTheme=\"@style/AppTheme.PopupOverlay\" />
成绩 2. Acivity代码: public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);
Button btn=(Button)findViewById(R.id.next);
btn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,DetailActivity.class);
startActivity(intent); } }); }
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();
//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }
return super.onOptionsItemSelected(item); } }
public class DetailActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activityl_detail); }
//noinspection SimplifiableIfStatement if (id == android.R.id.home) {
if (NavUtils.getParentActivityName(DetailActivity.this)!=null){ NavUtils.navigateUpFromSameTask(DetailActivity.this); }
return true; }else {
return super.onOptionsItemSelected(item); } } }
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务