一、Intent简介
二、代码
1.activity_main.xml
<RelativeLayout 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"
tools:context="com.example.s01_original_e05_intent.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/mBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnText"/>
</RelativeLayout>
2.activity_ohter.xml
<RelativeLayout 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"
tools:context="com.example.s01_original_e05_intent.OtherActivity" > <TextView
android:id="@+id/otherView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是另一个activity" />
</RelativeLayout>
3.MainActivity.java
package com.example.s01_original_e05_intent; import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button mBtn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtn = (Button) findViewById(R.id.mBtn);
mBtn.setOnClickListener(new btnListener());
} public class btnListener implements OnClickListener {
@Override
public void onClick(View v) {
/*Intent intent = new Intent();
intent.putExtra("extraKey", "Extra中的值123");
intent.setClass(MainActivity.this, OtherActivity.class);*/ //intent也可以在不同项目的activity间传递数据,如跳转到发短信
Uri uri = Uri.parse("smsto://10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "我当前的话费余额是多少?");
MainActivity.this.startActivity(intent);
} }
}
4.OtherActivity.java
package com.example.s01_original_e05_intent; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ohter);
Intent intent = getIntent();
TextView otherView = (TextView) findViewById(R.id.otherView);
otherView.setText(intent.getStringExtra("extraKey"));
}
}