標籤:android intent activity layout adt
使用意圖連結活動
1、建立一個名為“UsingIntent”的項目,右擊src檔案夾下的包名,選擇New-->Class選項,並將新的類檔案名稱命名為“SecondActivity”;
2、開啟AndroidManifest.xml檔案,添加如下代碼:
<activity android:name=".SecondActivity" android:label="Second Activity" > <!-- 新活動的意圖篩選器的名稱是net.zenail.SecondActivity,其它活動將通過這個名稱來調用這個活動 --> <!-- 意圖篩選器的類別是android.intent.category.DEFAULT,其它活動可以通過使用startActivity()方法啟動此活動 --> <intent-filter> <action android:name="net.zenail.SecondActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
3、在res/layout檔案夾下建立一個secondactivity.xml檔案,修改代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is the Second Activity!" /></LinearLayout>
4、開啟SecondActivity.java檔案,添加如下代碼,添加建立方法:
protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.secondactivity);}
5、在activity_main.xml檔案中添加如下代碼,建立一個Button:
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="Display second activity" />
6、在MainActivity.java中添加如下代碼,添加點擊方法:
public void onClick(View v) {startActivity(new Intent("net.zenail.SecondActivity"));// 將意圖篩選器的名稱傳進去// 如果要調用的活動是定義在同一個項目中,則可以重寫上面的方法: startActivity(new Intent(this,// SecondActivity.class));}
7、運行,效果如下:
點擊下載完整代碼~