ANDROID_MARS学习笔记_S01原始版_001_Intent

一、Intent简介

二、代码

1.activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.s01_original_e05_intent.MainActivity" >
10 
11     <TextView
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:text="@string/hello_world" />
15 
16     <Button
17         android:id="@+id/mBtn" 
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="@string/btnText"/>
21 </RelativeLayout>

2.activity_ohter.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.s01_original_e05_intent.OtherActivity" >
10 
11     <TextView
12         android:id="@+id/otherView"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="这是另一个activity" />
16 </RelativeLayout>

3.MainActivity.java

 1 package com.example.s01_original_e05_intent;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.net.Uri;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 
11 public class MainActivity extends Activity {
12 
13     private Button mBtn = null;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         mBtn = (Button) findViewById(R.id.mBtn);
19         mBtn.setOnClickListener(new btnListener());
20     }
21 
22     public class btnListener implements OnClickListener {
23         @Override
24         public void onClick(View v) {
25             /*Intent intent = new Intent();
26             intent.putExtra("extraKey", "Extra中的值123");
27             intent.setClass(MainActivity.this, OtherActivity.class);*/
28             
29             //intent也可以在不同项目的activity间传递数据,如跳转到发短信
30             Uri uri = Uri.parse("smsto://10086");
31             Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
32             intent.putExtra("sms_body", "我当前的话费余额是多少?");
33             MainActivity.this.startActivity(intent);
34         }
35         
36     }
37 }

4.OtherActivity.java

 1 package com.example.s01_original_e05_intent;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.widget.TextView;
 7 
 8 public class OtherActivity extends Activity {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_ohter);
14         Intent intent = getIntent();
15         TextView otherView = (TextView) findViewById(R.id.otherView);
16         otherView.setText(intent.getStringExtra("extraKey"));
17     }
18 }
原文地址:https://www.cnblogs.com/shamgod/p/5187325.html