




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗一:Android用戶界面(1)一、實驗?zāi)康?.熟悉Android應(yīng)用程序開發(fā)環(huán)境的構(gòu)建過程,了解Android應(yīng)用程序的文件結(jié)構(gòu),學會進行Android應(yīng)用程序開發(fā)。2.理解Android用戶界面的基本概念,掌握TextView、EditText、Button、ImageButton、Checkbox、RadioButton、Spinner、ListView等常用控件的使用。3.了解Android系統(tǒng)的6種界面布局,掌握線性布局、框架布局、表格布局、相對布局、絕對布局、網(wǎng)格布局等界面布局的使用。二、實驗內(nèi)容開發(fā)如圖所示的Android應(yīng)用程序。功能及要求。1) 在EditText中輸入數(shù)
2、據(jù),當點擊Input按鈕時,把輸入的信息在TestView上顯示;2) 當選擇CheckBox1時,在TestView上顯示“CheckBox1被選中”;當選擇CheckBox2時,在TestView上顯示“CheckBox2被選中”;當CheckBox1和CheckBox2均被選中時,在TestView上顯示“CheckBox1和CheckBox2被選中”;3) 當選擇RadioButton1時,在TestView上顯示“RadioButton1被選中”;當選擇RadioButton2時,在TestView上顯示“RadioButton1被選中”;RadioButton1和RadioButt
3、on2不能同時被選中;4) 點擊Close時,關(guān)閉應(yīng)用程序。三、試做步驟和參考代碼1.構(gòu)建實驗環(huán)境1)下載adt-bundle-windows-x86-20130917.467161976(集成Android4.3版本);2)安裝JDK;3)注冊環(huán)境變量。2.創(chuàng)建新的Android程序過程1)打開Android工程向?qū)В篎ileNewProjectAndroidAndroid Project;2)填寫工程名稱、選擇程序運行的Android系統(tǒng)版本、填寫應(yīng)用程序名稱3)點擊“Finish”按鈕,工程向?qū)鶕?jù)用戶所填寫的Android工程信息,自動在后臺創(chuàng)建Android工程所需要的基礎(chǔ)文件和目
4、錄結(jié)構(gòu)。3.編程實現(xiàn)實驗內(nèi)容【主要參考代碼】1)界面布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:id="+id/T
5、extView01"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="TextView01" ></TextView><EditText android:id="+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content
6、"android:text="Null" ></EditText><CheckBox android:id="+id/CheckBox01"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="CheckBox01" ></CheckBox><CheckBox android:id="+id/CheckBox02
7、" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="CheckBox02" > </CheckBox><RadioGroup android:id="+id/RadioGroup01" android:layout_width="wrap_content" android:layout_height="wrap_content&q
8、uot;><RadioButton android:id="+id/RadioButton01" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="RadioButton01" ></RadioButton><RadioButton android:id="+id/RadioButton02" android:layout_width=&q
9、uot;wrap_content" android:layout_height="wrap_content"android:text="RadioButton02" ></RadioButton></RadioGroup><Button android:id="+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"android:te
10、xt="Input" ></Button><Button android:id="+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Close" ></Button></LinearLayout>2)獲取各控件ID final TextView textView = (TextView)find
11、ViewById(R.id.TextView01); final EditText editText = (EditText)findViewById(R.id.EditText 01); final CheckBox checkBox1= (CheckBox)findViewById(R.id.CheckBox01); final CheckBox checkBox2= (CheckBox)findViewById(R.id.CheckBox02); final RadioButton radioButton1 = (RadioButton)findViewById(R.id.RadioBu
12、tton01); final RadioButton radioButton2 = (RadioButton)findViewById(R.id.RadioButton02); Button button1 = (Button)findViewById(R.id.Button01); Button button2 = (Button)findViewById(R.id.Button02);3)監(jiān)聽事件 CheckBox監(jiān)聽:CheckBox.OnClickListener checkboxListener = new CheckBox.OnClickListener()Overridepubl
13、ic void onClick(View v) switch(v.getId()case R.id.CheckBox01:textView.setText("CheckBox01, isChecked:"+String.valueOf(checkBox1.isChecked();return;case R.id.CheckBox02:textView.setText("CheckBox02, isChecked:"+String.valueOf(checkBox2.isChecked();return; RadioButton監(jiān)聽: RadioButto
14、n.OnClickListener radioButtonListener = new RadioButton.OnClickListener()Overridepublic void onClick(View v) switch(v.getId()case R.id.RadioButton01:textView.setText("RadioButton1 is selected");return;case R.id.RadioButton02:textView.setText("RadioButton2 is selected");return; ra
15、dioButton1.setOnClickListener(radioButtonListener);radioButton2.setOnClickListener(radioButtonListener); Button監(jiān)聽: Button1.setOnClickListener(new View.OnClickListener() public void onClick(View view) String msg = editText.getText().toString(); textView.setText(msg); ); Button2.setOnClickListener(new
16、 View.OnClickListener() public void onClick(View view) finish(); );四、實驗要求1.按時到指定實驗室進行實驗;2.學生應(yīng)獨立完成Android程序開發(fā)平臺的搭建;3.會創(chuàng)建新的Android應(yīng)用程序及編輯已創(chuàng)建的Android應(yīng)用程序;4.完成實驗內(nèi)容要求的Android應(yīng)用程序五、實驗報告實驗報告應(yīng)包含以下內(nèi)容:1.實驗?zāi)康模?.實驗內(nèi)容;3.Android應(yīng)用程序創(chuàng)建過程(工程名字用本人的姓或班內(nèi)序號+Experiment1組成);4.給出主要的程序代碼,如界面布局文件、邏輯代碼文件等中的實現(xiàn)代碼。5.在報告中給出程序運行界
17、面;6.應(yīng)有必要的總結(jié)和分析。實驗二:Android用戶界面(2)一、實驗?zāi)康?.掌握菜單資源的使用,能夠編程實現(xiàn)選項菜單、子菜單和快捷菜單。2.了解操作欄和Fragment的基本概念和使用方法。3.熟悉MVC模型中的控制器概念和界面事件,能夠處理單擊事件、按鍵事件、觸摸事件等界面事件的處理。二、實驗內(nèi)容開發(fā)如圖所示的Android應(yīng)用程序。 (1) (2) (3)功能及要求。1) 圖(1)所示為程序的初始界面,給出使用在XML文件中定義和邏輯代碼動態(tài)生成兩種菜單定義方法的實現(xiàn)代碼;2) 定義菜單的的點擊事件,該事件的處理過程是:若某個菜單子項被選中,在TextView控件顯示該菜單子項的名稱
18、和菜單ID;3) 圖(2)所示是使用XML文件方法實現(xiàn)菜單定義時,點擊“打印”子項時的用戶界面;4) 圖(3)所示是使用邏輯代碼動態(tài)生成方法實現(xiàn)菜單定義時,點擊“打印”子項時的用戶界面。三、試做步驟和參考代碼【主要參考代碼】1)XML實現(xiàn)方法<?xml version="1.0" encoding="utf-8"?><menu xmlns:android=" <item android:id="+id/main_menu_0" android:icon="drawable/pic0"
19、; android:title="打印" /> <item android:id="+id/main_menu_1" android:icon="drawable/pic1" android:title="新建" /> <item android:id="+id/main_menu_2" android:icon="drawable/pic2" android:title="郵件" /> <item android:id
20、="+id/main_menu_3" android:icon="drawable/pic3" android:title="設(shè)置" /> <item android:id="+id/main_menu_4" android:icon="drawable/pic4" android:title="訂閱" /></menu>2)獲取各控件IDfinal static int MENU_00 = Menu.FIRST;final static int
21、 MENU_01 = Menu.FIRST+1;final static int MENU_02 = Menu.FIRST+2;final static int MENU_03 = Menu.FIRST+3;final static int MENU_04 = Menu.FIRST+4; public boolean onCreateOptionsMenu(Menu menu) menu.add(0,MENU_00,0,"打印").setIcon(R.drawable.pic0); menu.add(0,MENU_01,1,"新建").setIcon(R
22、.drawable.pic1); menu.add(0,MENU_02,2,"郵件").setIcon(R.drawable.pic2); menu.add(0,MENU_03,3,"設(shè)置").setIcon(R.drawable.pic3); menu.add(0,MENU_04,4,"訂閱").setIcon(R.drawable.pic4); return true; 3)邏輯代碼 public boolean onOptionsItemSelected(MenuItem item) TextView label = (Text
23、View)findViewById(R.id.label); switch (item.getItemId() case MENU_00: label.setText("打印,菜單ID:" + item.getItemId(); return true; case MENU_01: label.setText("新建,菜單ID:" + item.getItemId(); return true; case MENU_02: label.setText("郵件,菜單ID:" + item.getItemId(); return true
24、; case MENU_03: label.setText("設(shè)置,菜單ID:" + item.getItemId(); return true; case MENU_04: label.setText("訂閱,菜單ID:" + item.getItemId(); return true; default: return false; 四、實驗要求1.按時到指定實驗室進行實驗;2.完成實驗內(nèi)容要求的Android應(yīng)用程序五、實驗報告實驗報告應(yīng)包含以下內(nèi)容:1.實驗?zāi)康模?.實驗內(nèi)容;3.Android應(yīng)用程序創(chuàng)建過程(工程名字用本人的姓或班內(nèi)序號+Exp
25、eriment2組成);4.給出主要的程序代碼,如界面布局文件、菜單定義文件、邏輯代碼文件等中的實現(xiàn)代碼。5.在報告中給出程序運行界面;6.應(yīng)有必要的總結(jié)和分析。實驗三:Intent及組件通信一、實驗?zāi)康?.了解使用Intent進行組件通信的原理。2.掌握使用Intent啟動Activity的方法。3.掌握獲取Activity返回值的方法。二、實驗內(nèi)容開發(fā)如圖所示的Android應(yīng)用程序。 (1) (2) (3)功能及要求。1) 圖(1)為程序的初始界面(父Activity),其主要功能是提供1個TextView和兩個Button,點擊“啟動Activity1”則啟動圖(2)所示的SubAct
26、ivity1(子Activity),點擊“啟動Activity2”則啟動圖(3)所示的SubActivity2(子Activity);2) SubActivity1包含1個TextView、1個EditText和兩個Button,TextView用于提示當前Activity是子Activity,EditText用于輸入返回父Activity的信息,點擊“接受”Button則關(guān)閉SubActivity1并把輸入信息返回,點擊“撤銷”Button則直接關(guān)閉SubActivity1;3) 當有信息從子Activity返回時,父Activity接收返回信息并把它顯示在TextView控件上;4) Su
27、bActivity2包含1個TextView、1個Button,TextView用于提示當前Activity是子Activity,Button則直接關(guān)閉SubActivity2。三、試做步驟和參考代碼【主要參考代碼】1)父Activity<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent
28、" android:layout_height="fill_parent" ><TextView android:id="+id/textShow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /><Buttonandroid:id="+id/btn1"android:layout_width="wra
29、p_content"android:layout_height="wrap_content"android:text="啟動Activity1"/><Buttonandroid:id="+id/btn2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="啟動Activity2"/></LinearLayout>2)Su
30、bActivity1<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:id="+id/textShow" and
31、roid:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SubActivity 1" /><EditTextandroid:id="+id/edit"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text=""/><
32、Buttonandroid:id="+id/btn_ok"android:layout_width="100dip"android:layout_height="wrap_content"android:text="接受"/><Buttonandroid:id="+id/btn_cancel"android:layout_width="100dip"android:layout_height="wrap_content"android:text
33、="撤銷"/>/></LinearLayout>3)SubActivity2<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >&l
34、t;TextView android:id="+id/textShow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SubActivity 2" /><Buttonandroid:id="+id/btn_return"android:layout_width="100dip"android:layout_height="wra
35、p_content"android:text="關(guān)閉"/></LinearLayout>4)子Activity注冊<activity android:label="string/app_name" android:name=".SubActivity1"> </activity><activity android:label="string/app_name" android:name=".SubActivity2"> </a
36、ctivity>4)SubActivity1定義public class SubActivity1 extends Activity Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.subactivity1); final EditText editText = (EditText)findViewById(R.id.edit); Button btnOK = (Button)findViewById(R
37、.id.btn_ok); Button btnCancel = (Button)findViewById(R.id.btn_cancel); btnOK.setOnClickListener(new OnClickListener() public void onClick(View view) String uriString = editText.getText().toString(); Uri data = Uri.parse(uriString); Intent result = new Intent(null, data); setResult(RESULT_OK, result)
38、; finish(); ); btnCancel.setOnClickListener(new OnClickListener() public void onClick(View view) setResult(RESULT_CANCELED, null); finish(); ); 5)SubActivity2定義public class SubActivity2 extends Activity /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstan
39、ceState) super.onCreate(savedInstanceState); setContentView(R.layout.subactivity2); Button btnReturn = (Button)findViewById(R.id.btn_return); btnReturn.setOnClickListener(new OnClickListener() public void onClick(View view) setResult(RESULT_CANCELED, null); finish(); ); 6)主要邏輯代碼public class Activity
40、CommunicationActivity extends Activity private static final int SUBACTIVITY1 = 1;private static final int SUBACTIVITY2 = 2;TextView textView; /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R
41、.layout.main); textView = (TextView)findViewById(R.id.textShow); final Button btn1 = (Button)findViewById(R.id.btn1); final Button btn2 = (Button)findViewById(R.id.btn2); btn1.setOnClickListener(new OnClickListener() public void onClick(View view) Intent intent = new Intent(ActivityCommunicationActi
42、vity.this, SubActivity1.class); startActivityForResult(intent, SUBACTIVITY1); ); btn2.setOnClickListener(new OnClickListener() public void onClick(View view) Intent intent = new Intent(ActivityCommunicationActivity.this, SubActivity2.class); startActivityForResult(intent, SUBACTIVITY2); ); Overridep
43、rotected void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data);switch(requestCode)case SUBACTIVITY1:if (resultCode = RESULT_OK)Uri uriData = data.getData();textView.setText(uriData.toString();break;case SUBACTIVITY2:break;四、實驗要求1.按時
44、到指定實驗室進行實驗;2.完成實驗內(nèi)容要求的Android應(yīng)用程序五、實驗報告實驗報告應(yīng)包含以下內(nèi)容:1.實驗?zāi)康模?.實驗內(nèi)容;3.Android應(yīng)用程序創(chuàng)建過程(工程名字用本人的姓或班內(nèi)序號+Experiment3組成);4.給出主要的程序代碼,如界面布局文件、菜單定義文件、邏輯代碼文件等中的實現(xiàn)代碼。5.在報告中給出程序運行界面;6.應(yīng)有必要的總結(jié)和分析。實驗四:Service及后臺服務(wù)一、實驗?zāi)康?.了解Service的原理和用途,掌握本地服務(wù)的管理方法。2.掌握服務(wù)的隱式啟動和顯式啟動方法。3.了解線程的啟動、掛起和停止方法,了解跨線程的界面更新方法。二、實驗內(nèi)容開發(fā)如圖所示的And
45、roid應(yīng)用程序。 (1) (2) (3)功能及要求。1)圖(1)為程序的初始界面,設(shè)計4個TextView和3個Button2)點擊“服務(wù)綁定”Button,則將已定義好的“MathService”服務(wù)綁定到本進程(“MathService”服務(wù)的功能是提供“+、-、*、/”四個運算);3)點擊“取消綁定”Button,則將 “MathService”服務(wù)與本進程的綁定取消;4)點擊“數(shù)學運算”Button,則首先生成兩個0-100的隨機數(shù),然后分別計算這兩個數(shù)的和、差、積和商,并分別顯示在4個TextView上;若點擊“數(shù)學運算”Button時,“MathService”服務(wù)未被綁定或已經(jīng)
46、解除了綁定,則顯示如圖(3)所示信息。三、試做步驟和參考代碼【主要參考代碼】1)界面設(shè)計<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView andro
47、id:id="+id/label1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="addResult"></TextView><TextView android:id="+id/label2" android:layout_width="fill_parent" android:layout_height="wrap
48、_content" android:text="subResult"></TextView><TextView android:id="+id/label3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="mulResult"></TextView><TextView android:id="+id/la
49、bel4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="divResult"></TextView><Button android:id="+id/bind" android:layout_width="wrap_content" android:layout_height="wrap_content"android
50、:text="服務(wù)綁定" ></Button><Button android:id="+id/unbind" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="取消綁定" ></Button><Button android:id="+id/compute" android:layout_width="
51、;wrap_content" android:layout_height="wrap_content"android:text="數(shù)學運算" ></Button></LinearLayout>2)服務(wù)定義public class MathService extends Serviceprivate final IBinder mBinder = new LocalBinder();public class LocalBinder extends BinderMathService getService() retu
52、rn MathService.this; Overridepublic IBinder onBind(Intent intent) Toast.makeText(this, "本地綁定:MathService", Toast.LENGTH_SHORT).show();return mBinder;Overridepublic boolean onUnbind(Intent intent) Toast.makeText(this, "取消本地綁定:MathService", Toast.LENGTH_SHORT).show(); return false;
53、public long Add(long a, long b)return a+b;public long Sub(long a, long b)return a-b;public long Mul(long a, long b)return a*b;public double Div(long a, long b)return (double)a/(double)b;3)服務(wù)注冊<service android:name=".MathService"/>4)主要邏輯代碼public class MainActivity extends Activity pri
54、vate MathService mathService;private boolean isBound = false;TextView addLable,subLable,mulLable,divLable; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addLable = (TextView)findViewById(R.id.label1); subLable = (TextView)findViewById(R.id.label2); mulLable = (TextView)findViewById(R.id.label3); divLable = (TextView)findViewById(R.id.label4); Button bindButton = (Button)findViewById(R.id.bind); Button unbindButton = (Button)findViewById(R.id.unbind); Button computButton = (Button)findViewB
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 預防醫(yī)學護理課件
- 項目管理課件
- 2025年電視節(jié)目自動播出設(shè)備項目發(fā)展計劃
- 2025年高純低羥基石英玻璃項目建議書
- 湘教版四年級上冊全冊音樂教案
- 2025年群路密碼機系列合作協(xié)議書
- 2025年HB步進電機合作協(xié)議書
- 2025年自裝卸補給車項目合作計劃書
- 文化會展服務(wù)行業(yè)概述
- 農(nóng)村一二三產(chǎn)業(yè)融合發(fā)展的農(nóng)村旅游產(chǎn)品創(chuàng)新與鄉(xiāng)村旅游目的地競爭力提升策略報告
- 生活垃圾衛(wèi)生填埋場運行管理方案
- 2022年全國《網(wǎng)絡(luò)安全學習教育知識》競賽試題庫與答案
- DL 5190.2-2019 電力建設(shè)施工技術(shù)規(guī)范 第2部分:鍋爐機組
- 印刷與人工智能集成
- 藥房盤盈盤虧分析報告
- 老年病科醫(yī)生工作總結(jié)
- (正式版)JBT 14933-2024 機械式停車設(shè)備 檢驗與試驗規(guī)范
- JJF 1033-2023 計量標準考核規(guī)范
- sqe年終述職報告
- 保密協(xié)議Word模板
- 支部政務(wù)課:加強年輕干部教育管理監(jiān)督的幾點體會
評論
0/150
提交評論