Android学习笔记 - Intent篇

1.Intent的主要作用
负责从一个Actvity传递数据到另一个Activity或其它对象
传递的对象不一定要是程序自身的Activity,可以是系统的,或其它程序的,或服务等


2.一个Intent对象包含的一组信息
Component name:传递到哪个对象
Action:传递的动作(Intent.ACTION_??)
Data:传递的URI
Extras:传递参数(多种数据类型的键值对)


3.调用跳转另一个Activity
使用startActivity(Intent)方法


4.添加监听事件的内部类
class btnGotoActivity02Listener implements OnClickListener {
  public void onClick(View v) {
    Intent intent = new Intent();
    intent.setClass(Activity01Activity.this, Activity02Activity.class); //从哪个对象传递到哪个对象
    intent.putExtra("text_content", "我是一只小小小小鸟"); //传键值对
    startActivity(intent); //跳转
  }
}


5.为按钮添加监听事件
btnGotoActivity02 = (Button) findViewById(R.id.btnGotoActivity02);
btnGotoActivity02.setOnClickListener(new btnGotoActivity02Listener());


6.使用Intent打开短信发送界面
class btnSendSmsListener implements OnClickListener {
  public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13800138000")); //SENDTO调用短信界面
    intent.putExtra("sms_body", "亲,我想你了……"); //短信内容
    startActivity(intent);
  }
}


7.另一个Activity中接收Intent传递过来的键值对
Intent intent = getIntent();
txtActivity02 = (TextView) findViewById(R.id.txtActivity02);

txtActivity02.setText(intent.getStringExtra("text_content"));


//完整布局示例

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btnGotoActivity02"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/btnGotoActivity02" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/btnSendSms"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="@string/btnSendSms" />  
  18.       
  19. </LinearLayout>  


//完整类示例

  1. public class Activity01Activity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     Button btnGotoActivity02 = null;  
  4.     Button btnSendSms = null;  
  5.   
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity01);  
  10.   
  11.         btnGotoActivity02 = (Button) findViewById(R.id.btnGotoActivity02);  
  12.         btnGotoActivity02.setOnClickListener(new btnGotoActivity02Listener());  
  13.           
  14.         btnSendSms = (Button) findViewById(R.id.btnSendSms);  
  15.         btnSendSms.setOnClickListener(new btnSendSmsListener());  
  16.     }  
  17.   
  18.     // 内部类:btnGotoActivity02点击事件监听  
  19.     class btnGotoActivity02Listener implements OnClickListener {  
  20.   
  21.         public void onClick(View v) {  
  22.             Intent intent = new Intent();  
  23.             intent.setClass(Activity01Activity.this, Activity02Activity.class);  
  24.             intent.putExtra("text_content""我是一只小小小小鸟");  
  25.             startActivity(intent);  
  26.         }  
  27.   
  28.     }  
  29.   
  30.     // 内部类:btnSendSms点击事件监听  
  31.     class btnSendSmsListener implements OnClickListener {  
  32.   
  33.         public void onClick(View v) {  
  34.             Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13800138000"));  
  35.             intent.putExtra("sms_body""亲,我想你了……");  
  36.             startActivity(intent);  
  37.         }  
  38.   
  39.     }  
  40. }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值