Android基礎Activity篇——Intent

來源:互聯網
上載者:User

標籤:項目   new   main   建立   start   vertica   使用   sch   onclick   

1.顯式的Intent

  intent是用來各各活動之間切換的,還可以用來傳遞參數。

  項目還是使用之前建立的ActivityTest項目,這裡建立一個活動SecondActivity.java,並且勾選建立second_layout.xml。

  在second_layout.xml。寫入代碼如下。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button_2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button 2"     /></LinearLayout>

  AS已經自動在AndroidMainfest.xml中自動註冊了該活動。

 <activity android:name=".SecondActivity"></activity>

 

  而且也在該活動中自動引入了second_layout.xml的布局。

setContentView(R.layout.second_layout);

2.在FirstActivity中onClick()方法添加代碼

 Intent intent=new Intent(FirstActivity.this,SecondActivity.class); startActivity(intent);

 使用Intent聲明並用構造方法建立一個Intent對象。

  Intent()構造方法中,有兩個參數。第一個是Context,即上下文,第二個是目標活動,Class類。

3.隱式Intent

  不明確指定目標活動,而是由系統自行分析,最後響應活動。

4.在AndroidMainfest.xml中添加代碼

<activity android:name=".SecondActivity">            <intent-filter>                <action android:name="com.example.activitytest.ACTION_START"/>  <!--表示該活動可以響應-->                <category android:name="android.intent.category.DEFAULT"/>      <!--設定類型為預設-->            </intent-filter></activity>

5.修改FirstActivity中按鈕的點擊事件

 Intent intent=new Intent("com.example.activitytest.ACTION_START");

  注意這裡Intent構造方法傳參的是一個action字串。

  注意:每一個Intet對象只能有一個action,但是可以有多個category。

intent.addCategory("com.example.activitytest.MY_CATEGORY");//通過此方法添加category

注意:只有當action與category都是匹配的,活動才能響應。如果不是程式會崩潰的。

 

Android基礎Activity篇——Intent

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.