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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值