標籤:項目 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