1.什麼是Intent
Intent是一種運行時綁定(run-time binding)機制,它能在程序運行過程中連接兩個不同的組件。通過使用Intent,程序可以向Android表達某種請求或者意願,Android會根據意願的內容選擇適當的組件來完成請求。
2.顯式Intent
顯式Intent指定了組件屬性的Intent,通過指定具體的組件類。實例如下
(1)新建佈局文件Xiaoxi.xml
<TabHost
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@android:id/tabhost"
android:layout_weight="1"
><!--引用android系統已有的id-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--定義第一個標籤頁的內容-->
<LinearLayout
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="消息頁面"
android:textSize="30sp"
/>
</LinearLayout>
</FrameLayout>
</TabHost>
(2).創建活動類XiaoXi.java
package xiaocool.net.my;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
/**
* Created by MRYU on 2015/3/7.
*/
public class XiaoXi extends TabActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xiaoxi);
//獲取tabhost組件
TabHost tabHost=getTabHost();
//創建第一個Tab頁
TabHost.TabSpec tab1=tabHost.newTabSpec("tab1")
.setIndicator("消息")//設置標題
.setContent(R.id.tab01);
//添加第一個tab頁
}
}
(3).註冊活動XiaoXi
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xiaocool.net.my" >
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".XiaoXi"></activity>
</application>
</manifest>
XiaoXi不是主活動,所以不需要配置intend-filter。
(4).測試顯示Intent
//監聽點擊事件
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//登錄信息合法性驗證
//判斷是否爲空
if(username.getText().toString().trim().length()==0||password.getText().toString().trim().length()==0){
Toast.makeText(MainActivity.this,"用戶名密碼不能爲空!", 1) .show();
return;
}
if(username.getText().toString().trim().equals("18653503680")&&password.getText().toString().trim().equals("cool")){
//讀取服務器端數據,與用戶填寫的信息比對,根據用戶名密碼返回信息
//登錄成功後填到系統主頁面
Intent intent=new Intent(MainActivity.this,XiaoXi.class);
startActivity(intent);
}
}
});
即可進入到消息界面。
3.Android內置的Action
Android內置了很多Action,比如ACTION_VIEW:數據顯示,ACTION_DIAL:撥打電話等
(1)修改按鈕點擊事件
//監聽點擊事件
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//跳轉到活動MyActivity2
//第一個參數:上下文,第二個參數:目標活動類
// Intent intent = new Intent(MyActivity.this,MyActivity2.class); //顯式Intent
// Intent intent = new Intent("com.yy.testactivity.MyActivity2.ACTION_START");//隱式Intent
// intent.addCategory("com.yy.testactivity.MY_CATEGORY");
Intent intent = new Intent(Intent.ACTION_VIEW); //內置Action
intent.setData(Uri.parse("http://www.baidu.com"));//跳轉到頁面
//啓動活動
startActivity(intent);
}
});
最終鏈接到百度。其他功能如下
3.1ACTION_MAIN
Android.intent.action.MAIN,在每個AndroidManifest.xml問洞中都能看到,標記當前的Activity作爲一個程序的入口。
3.2ACTION_DIAL
用於描述給用戶打電話的動作
3.3ACTION_PICK
從特定的一組數據中進行選擇數據操作
3.4ACTION_DEIT
編輯特定的數據
3.5ACTION_DELETE
刪除特定的數據
4.Intent的其他屬性
類別category:它爲執行動作的附加信息
數據類型type:顯式指定Intent的目標組件的類名稱。
組件component:指定Intent的目標組件的類名稱
附加信息extras:是其他所有附加信息的集合