Android開發-Intent和Activity

來源:互聯網
上載者:User

Intent的基本作用:

一個Intent對象包含了一組資訊:

1.      Component name

2.      Action

3.      Date

4.      Category

5.      Extras

6.      Flags

 

Intent 概述

• Intent是Android的核心組件,利用訊息實現應用程式間的互動機制,這種訊息描述了應用中一次操作的動作、資料以及附加資料,系統通過該Intent的描述負責找到對應的組件,並將Intent傳遞給調用的組件,完成組件的調用。
• Intent由動作、資料、分類、類型、組件和擴充資訊等內容組成,每個組成都由相應的屬性進行表示,並提供設定和擷取相應屬性的方法 

屬性 設定屬性方法 擷取屬性方法
動作 Action setAction() getAction()
資料 Data setData() getData()
分類 Category setCategory()  
類型 Type setType() getType()
組件 Component setComponent()
setClass()
setClassName()
getComponent()
擴充資訊 Extra putExtra() getXXXExtra()擷取不同資料類型的資料,如int類型則使用getIntExtra(),字串則使用getStringExtra()
getExtras()擷取Bundle包
 

 


 

在Android中,Intent和Activity是直接相互操作的。Intent的最常見的用途是繫結應用程式組件。Intent用來在應用程式的Activity間啟動、停止和傳輸。

為了開啟應用程式中的不同的介面(對應一個Activity),調用startActivity,傳入一個Intent,如:startActivity(myIntent);

由我們可以大致瞭解Activity和Intent之間的關係。

下面通過一個執行個體來說明如何利用Intent對Activity進行操作的:

1.建立一個Android項目(Activity02)

代碼清單分別為:

1)Android02.java

2)otherActivity.java

3)  main.xml

4)other.xml

5) string.xml

6)Android02Manifest.xml

 

=》》Android02.java

[java] <span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02; 
 
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 Activity02 extends Activity { 
    /** Called when the activity is first created. */ 
    private Button myButton = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        myButton = (Button)findViewById(R.id.myButton); 
        myButton.setText("myButton"); 
        myButton.setOnClickListener(new MyButtonListener()); 
    } 
     
  
 
    class MyButtonListener implements OnClickListener{ 
 
        public void onClick(View v) { 
            // TODO Auto-generated method stub  
            //產生一Intent對象  
            Intent intent = new Intent(); 
            intent.setClass(Activity02.this,otherActivity.class); 
            Activity02.this.startActivity(intent); 
        } 
             
        } 
  
}</span> 
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;

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 Activity02 extends Activity {
    /** Called when the activity is first created. */
 private Button myButton = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myButton = (Button)findViewById(R.id.myButton);
        myButton.setText("myButton");
        myButton.setOnClickListener(new MyButtonListener());
    }
   
 

 class MyButtonListener implements OnClickListener{

  public void onClick(View v) {
   // TODO Auto-generated method stub
   //產生一Intent對象
   Intent intent = new Intent();
   intent.setClass(Activity02.this,otherActivity.class);
   Activity02.this.startActivity(intent);
  }
   
  }
 
}</span>

 

=》》otherActivity.java

[java]
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
 
 
public class otherActivity extends Activity{ 
    private TextView myTextView = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.other); 
        myTextView = (TextView)findViewById(R.id.myTextView); 
        myTextView.setText(R.string.other); 
    } 
     
 

</span> 
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class otherActivity extends Activity{
 private TextView myTextView = null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.other);
  myTextView = (TextView)findViewById(R.id.myTextView);
  myTextView.setText(R.string.other);
 }
 

}
</span>
 

=》》main.xml

[html]
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
<Button  
    android:id="@+id/myButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"   
    /> 
 
</LinearLayout></span> 
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<Button
    android:id="@+id/myButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    />

</LinearLayout></span>

 

=》》other.xml

[html]
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> 
<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:id="@+id/myTextView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 
</span> 
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/myTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>
</span>

 

=》》string.xml

 

[html]
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
    <string name="hello">Hello World, Activity02!</string> 
    <string name="app_name">Activity02</string> 
    <string name="other">otherActivity</string> 
 
</resources></span> 
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, Activity02!</string>
    <string name="app_name">Activity02</string>
    <string name="other">otherActivity</string>

</resources></span>

 

=》》Android02Manifest.xml

[html]
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.Activity02" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk android:minSdkVersion="4" /> 
 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:name=".Activity02" 
            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=".otherActivity" android:label="@string/other"/> 
    </application> 
 
</manifest></span> 


  模擬器運行結果:


按下Button按鈕就執行otherActivity







摘自 wwj_748

 


聯繫我們

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