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"));


//完整布局示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnGotoActivity02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnGotoActivity02" />

    <Button
        android:id="@+id/btnSendSms"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnSendSms" />
    
</LinearLayout>


//完整类示例

public class Activity01Activity extends Activity {
	/** Called when the activity is first created. */
	Button btnGotoActivity02 = null;
	Button btnSendSms = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity01);

		btnGotoActivity02 = (Button) findViewById(R.id.btnGotoActivity02);
		btnGotoActivity02.setOnClickListener(new btnGotoActivity02Listener());
		
		btnSendSms = (Button) findViewById(R.id.btnSendSms);
		btnSendSms.setOnClickListener(new btnSendSmsListener());
	}

	// 内部类:btnGotoActivity02点击事件监听
	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);
		}

	}

	// 内部类:btnSendSms点击事件监听
	class btnSendSmsListener implements OnClickListener {

		public void onClick(View v) {
			Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13800138000"));
			intent.putExtra("sms_body", "亲,我想你了……");
			startActivity(intent);
		}

	}
}


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值