第一个Activity
package com.example.activity01;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
class MyButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//生成一个intent 对象
Intent intent=new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
intent.putExtra("testIntent", "123");
intent.putExtra("你妹","你妹的");
MainActivity.this.startActivity(intent);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTextView=(TextView)findViewById(R.id.myTestView);
Button myButton=(Button)findViewById(R.id.myButton);
myTextView.setText("我的第一个文本");
myButton.setText("我的第一个按钮"+"\n"+"test");
myButton.setOnClickListener(new MyButtonListener());
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
接受数据的Activity
package com.example.activity01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class OtherActivity extends Activity{
private TextView myText2View=null;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Intent intent=getIntent();
String value=intent.getStringExtra("testIntent");
String value2=intent.getStringExtra("你妹");
myText2View=(TextView)findViewById(R.id.myText2View);
myText2View.setText(value);
myText2View.setText(value2);
}
}
第一个的布局
<LinearLayout
xmlns: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"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/myTestView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
第二个的布局
<LinearLayout
xmlns: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"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/myText2View"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Manifest注册文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity01"
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.activity01.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="com.example.activity01.OtherActivity"
android:label="@string/other"
android:theme="@android:style/Theme.Dialog"
/>
</application>
</manifest>