Android學習筆記(十)——使用意圖連結活動

來源:互聯網
上載者:User

標籤: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、運行,效果如下:



點擊下載完整代碼~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.