一、什麼是Intentjava
Intent能夠理解爲信使(意圖)android
由Intent來協助完成Android各個組件之間的通信,即Android四大組件之間的關聯是經過Intent來實現。app
二、Intent實現頁面之間的跳轉ide
無回傳的跳轉:直接啓動新頁面this
startActivity(intent)code
有返回值的跳轉:新頁面能夠回傳信息給舊頁面xml
startActivityForResult(intent,requestCode);對象
須要注意,這種方式須要關聯另外兩個方法:事件
在新頁面中,須要使用setResult(resultCode,data)方法,用於回傳給舊頁面信息utf-8
在舊頁面中,須要使用onActivityResult(int requestCode,int resultCode,intent data)方法,用於接收新頁面回傳的信息。
三、startActivity方法實現無返回值的頁面跳轉
建立初始頁面的FActivity.java文件:
package com.example.myintentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FActivity extends Activity{ private Button bt1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.factivity); // 經過點擊bt1實現頁面之間的跳轉:stratActivity方式,須要初始化Intent bt1=(Button) findViewById(R.id.button1_first); // 註冊點擊事件 bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 第一個參數,上下文對象,第二個參數,目標文件。在匿名內部類中,沒法訪問當前類中的對象, // 須要代表FActivity.this,或者事先聲明 // 初始化一個意圖 Intent intent =new Intent(FActivity.this, SActivity.class); // 用Intent這個方法去實現這個意圖 startActivity(intent); } }); } }
建立其對應的factivity.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/button1_first" style="?android:attr/buttonStyleSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一種啓動方式" /> <Button android:id="@+id/button2_second" style="?android:attr/buttonStyleSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二種啓動方式" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="吧第二個頁面回傳的數據顯示出來" /> </LinearLayout>
建立待跳轉的新頁面的SActivity文件:
package com.example.myintentdemo; import android.app.Activity; import android.os.Bundle; public class SActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); } }
建立其對應的sactivity.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/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
在AndroidManifest.xml文件中對上述兩個頁面進行註冊,並將Factivity.java設置爲程序入口:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myintentdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.myintentdemo.MainActivity" android:label="@string/app_name" > </activity> <activity android:name="com.example.myintentdemo.FActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.myintentdemo.SActivity" android:label="@string/app_name" > </activity> </application> </manifest>
四、startActivityForResult實現有返回值的頁面跳轉
修改FActivity.java文件以下:
package com.example.myintentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class FActivity extends Activity{ private Button bt1; private Button bt2; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.factivity); tv=(TextView) findViewById(R.id.textView1); // 經過點擊bt1實現頁面之間的跳轉:stratActivity方式,須要初始化Intent bt1=(Button) findViewById(R.id.button1_first); bt1=(Button) findViewById(R.id.button2_second); // 註冊點擊事件 bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 第一個參數,上下文對象,第二個參數,目標文件。在匿名內部類中,沒法訪問當前類中的對象, // 須要代表FActivity.this,或者事先聲明 Intent intent =new Intent(FActivity.this, SActivity.class); startActivity(intent); } }); bt2.setOnClickListener(new OnClickListener() { // 經過startActivityForResult方法實現 @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent =new Intent(FActivity.this, SActivity.class); // 第一個參數是intent對象,第二個參數是請求的一個標誌 startActivityForResult(intent, 1); } }); } // 經過startActivityForResult跳轉,接收返回數據的方法 // requestCode請求的標誌 // resultCode第二個頁面返回的標誌 // data第二個頁面回傳的數據 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode==1&&resultCode==2){ String content=data.getStringExtra("data"); tv.setText(content); } } }
修改SActivity.java文件以下:
package com.example.myintentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SActivity extends Activity{ private Button bt; private String content="你好"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); // 第二個頁面何時給第一個頁面回傳數據,被動式 // 回傳到第一個頁面其實是一個Intent對象 bt=(Button) findViewById(R.id.button1); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View V) { // TODO Auto-generated method stub Intent data=new Intent(); data.putExtra("data",content); setResult(2,data); // 結束當前頁面,銷燬,返回第一個頁面 finish(); } }); } }