还剩4页未读,继续阅读
文本内容:
实验
(一)实验名称Activity及UIWindows软件环境EllipseAndroid SDKPC硬件环境实验目的完成应用程序建立及界面处理技术Android完成应用程序建立及界面处理技术,包括以下内容:Android应用层程序建立•Android•界面布局及控件添加•菜单添加一一选项菜单及上下文菜单•消息处理使用•Array Adapter实验内容(应包括实验题目、实验要求、实验任务等)实验要求:完成“toDoList”功能实验步骤:
1.建立Android应用程序框架
2.添加控件一一Ed添Text负责输入“工作项目”;ListView负责显示“工作列表”
3.定义ArrayList,并通过ArrayAdapter将ListView与ArrayList相关联
4.添加选项菜单a.当EditText控件Visible时,有两个菜单项,“添加项目”和“取消添加”其中添加项目将EditText内容添加到ListView中,“取消项目”将EditText隐藏b.当EditText控件隐藏时,有一个菜单项,及“添加菜单”项,当按下该菜单时,EditText由隐藏状态改为显示状态
5.为EditText添加按键事件(D-pad键)监听器,当按下该键时,EditText内容添加到“工作列表”中
6.为ListView添加上下文菜单,当长按ListView某一项时,跳出上下文菜单,提示用户可以删除该项目实验过程toDoList功能实现package com.paad.todolist;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.view.ContextMenu;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnKeyListener;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.EditText;import android.widget.ListView;public classToDoList extendsActivity{static Analprivate intADD_NEW_TODO=Menu.FIRST;static Analprivate intREMOVE_TODO=Menu.FIRST+1;private booleanaddingNew=false;private ArrayListStringtodoltems;private ListViewmyListView;private EditTextmyEditText;private ArrayAdapterStringaa;public voidonCreateBundle savedlnstanceState{super.onCreatesavedlnstanceState;setContent ViewR.layout.main;myListView=ListViewfindViewByldR.id.myListView;myEditText=EditTextfindViewByldR.id.myEditText;todoltems=new ArrayListString;aa=new ArrayAdapterStringthis,android.R.layout.simple_Iist_item_l,todoltems;myListView.setAdapteraa;myEditText.setOnKeyListenernew OnKeyListener{public booleanonKeyView v,int keyCode,KeyEvent event{if event.getAction==KeyEvent.ACTION_DOWNif keyCode==KeyEvent.KEYCODE_DPAD_CENTER{todoltems.add0,myEditText.getText.toString;myEditText.setTextnn;aa.notifyDataSetChanged;cancelAddQ;return true;}return false;}};registerForContextMenumyListView;@Overridepublic booleanonCreateOptionsMenuMenu menu{super.onCreateOptionsMenumenu;Menuitem itemAdd=menu.add0,ADD_NEW_TODO,Menu.NONE,R.string.add_new;Menuitem itemRem=menu.add0REMOVE_TODO,Menu.NONE,R.string.remo ve;9itemAdd.setIconR.drawable.add_new_item;itemRem.setIconR.drawable.remove_item;itemAdd.setShortcutfO\fa*;itemRem.setShortcut*r,fr*;return true;@Overridepublic booleanonPrepareOptionsMenuMenu menu{super.onPrepareOptionsMenumenu;int idx=myListView.getSelectedItemPosition;String removeTitle=getStringaddingNewR.string.cancel:R.string.remove;Menuitem removeitem=menu.findItemREMOVE_TODO;removeltem.setTitleremoveTitle;removeItem.setVisibleaddingNew||idx-1;return true;@Overridepublic voidonCreateContextMenuContextMenu menu,View ContextMenu.ContextMenuInfomenuinfo{super.onCreateContextMenumenu,v,menuinfo;menu.setHeaderTitle**Selected To Do Item;menu.add0REMOVE_TODO,Menu.NONE,R.string.remove;9@Overridepublic booleanonOptionsItemSelectedMenuItem item{super.onOptionsItemSelecteditem;int index=myListView.getSelectedItemPosition;switch item.getltemldO{case REMOVE.TODO:{if addingNew{canceIAdd;else{}removeltemindex;return true;case ADD_NEW_TODO:{addNewItem;return true;}}return false;@Overridepublic booleanonContextItemSelectedMenuItem item{super.onContextltemSelecteditem;switch item.getltemldO{case REMOVE_TODO:{AdapterView.AdapterContextMenuInfo menuinfo;menuinfo=AdapterView.AdapterContextMenuInfoitem.getMenuInfo;int index=menulnfo.position;removeltemindex;return true;return false;private voidcancelAddQ{addingNew=false;myEditTextsetVisibilityView.GONE;}private voidaddNewItem{addingNew=true;myEditTextsetVisibilityView.VISIBLE;my EditText.requestFocus;private voidremoveltemint_index{todoltems.remove_index;aa.notifyDataSetChanged;private voidcancelAdd{addingNew=false;myEditText.setVisibilityView.GONE;private voidaddNewItem{addingNew=true;myEditTextsetVisibilityView.VISIBLE;myEditText.requestFocus;private voidremoveltemint_index{todoltems.remove_index;aa.notifyDataSetChanged;}界面布局xml version=n
1.0n encoding=uutf-8HLinearLayout xmlns:android=nnandroid:orientation=verticalandroid:layout_width=Mfill_parentnandroid:layout_height=nfill_parentnEditTextandroid:id=H@+id/myEditTextHandroid:Iayout_width=fill_parentHandroid:layout_height=Hwrap_contentu android:text=nNew ToDo Item”/ListViewandroid:id=@+id/myListViewandroid:layout_width=Hfill_parentHandroid:layout_height=Hwrap_contentn//LinearLayout实验结果:1•_.•_Jr0o|1234568Q WE RT]Y u|I0pS|DF」G*H JK LDELZ]z XC VB NM*@/u.1SYM3:52To-do ListNewToDoItemAdd NewItem。