Ref: http://www.linuxidc.com/Linux/2011-04/34228.htm
Android用Intent這個特殊類實現在Activity與Activity之間的切換。通過解析各種intent,從一個屏幕導航到另一個屏幕是很簡單的。當向前導航時,activity將會調用 startActivity(IntentmyIntent)方法。例子效果如下:
IntentTest.java代碼如下:
- package intent.pack;
- 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.EditText;
- public class IntentTest extends Activity {
- /** Called when the activity is first created. */
- private EditText et1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- et1=(EditText)findViewById(R.id.editText1);
- Button bt=(Button)findViewById(R.id.button1);
- bt.setOnClickListener(new ButtonListener());
- }
- class ButtonListener implements OnClickListener{
- @Override
- public void onClick(View arg0) {
- String val=et1.getText().toString();
- //生成一個Intent對象
- Intent intent=new Intent();
- //設置傳遞的參數
- intent.putExtra("val", val);
- //從Activity IntentTest跳轉到Activity IntentTest01
- intent.setClass(IntentTest.this,IntentTest01.class);
- //啓動intent的Activity
- IntentTest.this.startActivity(intent);
- }
- }
- }
IntentTest01.java代碼如下:
- package intent.pack;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- public class IntentTest01 extends Activity{
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.intent01);
- Intent intent=getIntent();
- String value=intent.getStringExtra("val");
- TextView et=(TextView)findViewById(R.id.textView1);
- et.setText(value);
- }
- }
main.xml代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:text="請輸入內容!" android:layout_height="wrap_content"></EditText>
- <Button android:id="@+id/button1" android:text="確定" android:layout_height="wrap_content" android:layout_width="match_parent"></Button>
- </LinearLayout>
intent01.xml代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView android:id="@+id/textView1" android:textSize="20sp" android:layout_width="match_parent" android:text="請輸入內容!" android:layout_height="wrap_content"></TextView>
- </LinearLayout>
AndroidManifest.xml代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="intent.pack"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="4" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".IntentTest"
- 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=".IntentTest01"
- android:label="@string/app1_name">
- </activity>
- </application>
- </manifest>