一個(gè)activity就好比一個(gè)網(wǎng)頁(yè),此文章講解怎樣創(chuàng)建一個(gè)activity并且實(shí)現(xiàn)跳轉(zhuǎn)!
開(kāi)發(fā)環(huán)境:android4.1.1
實(shí)驗(yàn)功能:
在第一個(gè)Hello World!為標(biāo)簽的activity中顯示good,該界面中有一個(gè)名為Next的按鈕。點(diǎn)擊Next按鈕進(jìn)入到第二個(gè)activity中去,第二個(gè)界面中只有1個(gè)Close按鈕。當(dāng)然,據(jù)網(wǎng)上有人將要比較安全的實(shí)現(xiàn)關(guān)閉程序的功能也不是挺簡(jiǎn)單的,因?yàn)閍ndroid有專(zhuān)門(mén)的退出鍵返回鍵等。所以該Close按鈕暫時(shí)沒(méi)去實(shí)現(xiàn)它。
我的第1個(gè)activity為HelloworldActivity,第2個(gè)activity為NextActivity.
實(shí)驗(yàn)說(shuō)明:
1. 要實(shí)現(xiàn)從1個(gè)activity跳到另一個(gè)activity,這需要通過(guò)intent來(lái)實(shí)現(xiàn)。當(dāng)然我們需要在Next按鈕上綁定一個(gè)按鈕按下的監(jiān)聽(tīng)器(這些好像是java中的知識(shí),可我從沒(méi)學(xué)過(guò)java,只能用到哪個(gè)地方再去學(xué)了),一旦該按鈕監(jiān)聽(tīng)到有按鍵按下,則通過(guò)intent將指定的第2個(gè)activity觸發(fā),這樣就完成了本次試驗(yàn)的功能。
2.在工程中,每一個(gè)activity都對(duì)應(yīng)一個(gè)xml文件,xml文件主要是控制各控件的位置和屬性的.
3. asserts目錄下可以存放任何文件,res目錄下也可以存放任意文件,且res下的文件會(huì)在gen目錄下的R.java文件中自動(dòng)生成一個(gè)全局id。
4. res目錄下的values目下的strings.xml中的控件也是每個(gè)控件都在R.jar中對(duì)應(yīng)一個(gè)id號(hào)。當(dāng)然layout下的main.xml文件也是一樣的。
5. AndroidManifest.xml是整個(gè)應(yīng)用程序的配置文件。
6. android.jar是該程序應(yīng)用的所有android類(lèi)的來(lái)源。
7. view是android中所有控件的父類(lèi)。
8. Activity可以理解為人機(jī)交互的界面,也可以理解為一個(gè)控件的容器。
9. eclipse中用crtl+shift+c注釋選中區(qū)域,同時(shí)也是用ctrl+shift+c取消選中區(qū)域,這里的注釋為雙斜桿//.
如果用/**/來(lái)注釋的話,就是用ctrl+shift+/來(lái)注釋選中區(qū)域,用ctrl+shift+\來(lái)取消選中區(qū)域的注釋。
10. 用alt+/是增加單詞函數(shù)等補(bǔ)全功能的提示。
11. ctrl+shift+o可以自動(dòng)添加eclipse中檢測(cè)到需要導(dǎo)入的包文件。
12. setText里面不能采用資源引用,資源引用顯示文本應(yīng)該是在xml中的。
13. xml的注釋不能出現(xiàn)在屬性值代碼中,不能出現(xiàn)在標(biāo)記中。且注釋格式為<!--注釋內(nèi)容-->
14. xml語(yǔ)句結(jié)束后并不需要結(jié)束符號(hào),比如說(shuō)分號(hào)。
試驗(yàn)結(jié)果(在模擬器中運(yùn)行的):
啟動(dòng)程序后:
實(shí)驗(yàn)主要部分代碼及注釋?zhuān)?/p>
HelloworldActivity.java: package com.example.helloworld; import android.app.Activity; import android.content.Intent; import android.view.View;//注意view的大小寫(xiě) import android.view.View.OnClickListener; import android.os.Bundle; import android.widget.Button; public class HelloworldActivity extends Activity { private Button my_button = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_helloworld); my_button = (Button)findViewById(R.id.my_button); my_button.setText( "Next" ); my_button.setOnClickListener(new MyButtonListener()); } class MyButtonListener implements OnClickListener{ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(HelloworldActivity.this, NextActivity.class); HelloworldActivity.this.startActivity(intent); } } /** * 如果下面的語(yǔ)句不要,那么系統(tǒng)運(yùn)行的時(shí)候會(huì)直接進(jìn)入本程序中,而不是先進(jìn)入主菜單 * 再進(jìn)入選擇應(yīng)用程序界面進(jìn)入本程序 * 為了方便調(diào)試,這里就不進(jìn)入主菜單界面了*/ /*@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_helloworld, menu); return true; }*/ } NextActivity.java: package com.example.helloworld; import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class NextActivity extends Activity{ private Button my_button2 = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); my_button2 = (Button)findViewById(R.id.my_button2); // my_button2.setText("@string/close"); //setText里面不能采用資源引用 //資源引用顯示文本應(yīng)該是在xml中的 my_button2.setText("Close"); } }
<!-- android:text="@string/wuwei" --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="false" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="@string/wuwei" tools:context=".HelloworldActivity" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/my_button" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </RelativeLayout> activity_next.xml: <?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" > <Button android:id="@+id/my_button2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".HelloworldActivity" android:label="@string/hello_world" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NextActivity" android:label="@string/close"> </activity> </application> </manifest>
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答??! 點(diǎn)擊進(jìn)入論壇