開啟多個Activity以及多個Activity之間的引數傳遞
Strings.xml
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="app_name">moreActivity</string>
<stringname="action_settings">Settings</string>
<stringname="button">開啟新的視窗</string>
<stringname="title">新視窗</string>
<stringname="content"
<stringname="otherButton">關閉</string>
</resources>
主Activity介面
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:onClick="openActivity"/>
</RelativeLayout>
另一個Activity介面<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:id="@+id/label"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/otherButton"
android:onClick="close"
/>
</LinearLayout>
AndroidManifest.xml檔案把新的Activity新增進來<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moreactivity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.moreactivity.MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--把新的Activity新增進來-->
<activityandroid:name=".OtherActivity"
android:label="@string/title"></activity>
</application>
</manifest>
主Activity
packagecom.example.moreactivity;
importandroid.os.Bundle;
importandroid.annotation.SuppressLint;
importandroid.app.Activity;
importandroid.content.ComponentName;
importandroid.content.Intent;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.Toast;
publicclassMainActivity extendsActivity {
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
}
publicvoidopenActivity(View v) {
Intent intent =null;
//方式一
/*
* { intent = newIntent(); //設定要啟用的元件//第一個引數:上下文
*//第二個引數:要啟用Activity的名稱intent.setClass(this,OtherActivity.class); }
*/
//方式二
/*
* { intent = newIntent(); intent.setClassName(this,
*"com.example.moreactivity.OtherActivity"); }
*/
//方式三
/*
* { intent = newIntent(); //元件的形式ComponentNamecomponent = new
*ComponentName(this, OtherActivity.class);
*intent.setComponent(component); }
*/
//方式四
/*
* { intent = newIntent(this, OtherActivity.class); }
*/
//注:方式一到四隻能啟用本應用的eActivity
//而方式五可以跨應用啟用
//方式五
{
intent = newIntent();
intent.setClassName("com.example.moreactivity","com.example.moreactivity.OtherActivity");
}
//利用意圖物件傳遞引數
/*
*intent.putExtra("name", "darren");intent.putExtra("age", 23);
*/
//批量傳遞
Bundle bundle = newBundle();
bundle.putString("name","zhengzhou");
bundle.putInt("age",100);
intent.putExtras(bundle);
//startActivity(intent);
//requestCode請求碼
//startActivityForResult(intent, requestCode);
startActivityForResult(intent,200);
}
@Override
protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) {
String result =data.getStringExtra("result");
Toast.makeText(this,result, Toast.LENGTH_LONG).show();
}
}
另一個Activty
packagecom.example.moreactivity;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.TextView;
publicclassOtherActivity extendsActivity {
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
//TODOAuto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.otheractivity);
//得到的意圖物件就是用於啟用這個Activity的意圖物件
Intent intent =this.getIntent();
/*
* String name =intent.getStringExtra("name");
*//第二個引數表示預設值,如果取age取不到的話,就返回0int age = intent.getIntExtra("age", 0);
*/
Bundle bundle =intent.getExtras();
String name =bundle.getString("name");
intage = bundle.getInt("age");
TextView textView =(TextView) this.findViewById(R.id.label);
textView.setText("姓名:"+ name + " 年齡:"+ age);
}
publicvoidclose(View v) {
//resultCode結果碼錶示來自於當前Activity的那一部分資料
//setResult(resultCode, data)
Intent data = newIntent();
data.putExtra("result","new Actiovity closed");
this.setResult(30,data);
//關閉當前Avtivity
this.finish();
}
}
執行結果如圖所示: