Android入门学习笔记(五):Activity与Intent

Android入门学习笔记(五):Activity与Intent

.多个Activity之间的关系

一个程序一般都是含有多个Activity,如一个应用程序中含有两个Activity:

当我们需要从Activity01跳转到Activity02时,就需要调用Activity01的startActivity方法,并传入一个Intent对象,并由这个Intent对象决定跳转的是哪一个Activity、并传入相应信息

.Intent的基本作用

Intent在Android帮助文档里的介绍如下:

An Intent object is a bundle of information. It contains information of interest to the component that receives the intent (such as the action to be taken and the data to act on) plus information of interest to the Android system (such as the category of component that should handle the intent and instructions on how to launch a target activity). Principally, it can contain the following:


一个Intent对象包含了一组信息:

1. Component name                    

2. Action                                         

3. Data

4. Category

5. Extras

6. Flags 


Component name:  组件名字,决定跳转时启动哪一Activity(也可以启动Service等其他组件)


Action:  跳转到另一个Activity所做的动作

在官方API中指定了如下Action常量

 

如,指定Action为ACTION_CALL时,则初始化一个电话

Data:传送的数据:URI 。 The URI of the data to be acted on and the MIME type of that data

Extras:额外的资源:键值对

Category和Flags暂不介绍

.在一个Activity中启动另外一个Activity并传递相应数据

一个应用程序如下

OneActivity

package zhang.activity;

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;
import android.widget.TextView;

public class OneActivity extends Activity {
    /** Called when the activity is first created. */
    
    
    private Button myButton = null;
    private TextView myTextView = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myButton = (Button)findViewById(R.id.myButton);
        myTextView = (TextView)findViewById(R.id.myTextView);
        myButton.setOnClickListener(new MyButtonListener());
    }
    
    class MyButtonListener implements OnClickListener{

		public void onClick(View v) {
			Intent  intent = new Intent();
			intent.putExtra("hello",OneActivity.this.myTextView.getText());
			intent.setClass(OneActivity.this, OtherActivity.class);
			OneActivity.this.startActivity(intent);
			
		}
    	
    	
    }
}
OtherActivity
package zhang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivity extends Activity {
	
	TextView myTextView = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		
		Intent intent = getIntent();
		String value = "Received:" + intent.getStringExtra("hello");
		myTextView = (TextView)findViewById(R.id.myTextView);
		myTextView.setText(value);
	}
	
	
}
OneActivity的布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/myTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button
	android:id="@+id/myButton"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	/>
</LinearLayout>

OtherActivity的布局文件other.xml

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

<TextView  
	android:id="@+id/myTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    
    />

</LinearLayout>

在OneActivity中通过findViewByID获得对应的TextView控件和Button控件,并为Button控件注册OnClickListener监听器

在OnClickListener中复写onClick方法:

public void onClick(View v) {
			Intent  intent = new Intent();
			intent.putExtra("hello",OneActivity.this.myTextView.getText());
			intent.setClass(OneActivity.this, OtherActivity.class);
			OneActivity.this.startActivity(intent);
			
		}
在其中定义了一个Intent对象,intent.putExtra传出了一个键值对:hello--OneActivity.this.myTextView.getText()

intent.setClass设置了跳转方式为:从该Activity跳转到OtherActivity


在OtherActivity中

Intent intent = getIntent();
		String value = "Received:" + intent.getStringExtra("hello");
		myTextView = (TextView)findViewById(R.id.myTextView);
		myTextView.setText(value);
通过getIntent()方法获得了一个Intent方法,并定义了字符串对象value为 "Received: " + intent.getStringExtra("hello");

即Received加上通过Intent传入的键值对"hello"的值


所以我们看到的程序一开始界面为


点击Button按钮后会跳转到下面Activity,并能看到TextView的文字被设置为了Received:Hello Worl,activity02!



. 非同一个应用程序中的Activity也可以的跳转

如将上面的复写OneActivity中的OnClick(View v)方法的内容改为如下
public void onClick(View v) {
			
			/*Intent  intent = new Intent();
			intent.putExtra("hello",OneActivity.this.myTextView.getText());
			intent.setClass(OneActivity.this, OtherActivity.class);
			OneActivity.this.startActivity(intent);
			*/
			
			Uri uri = Uri.parse("smsto:10010");
			Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
			intent.putExtra("sms_body", "This is the sms text");
			startActivity(intent);
		}

则点击Button按钮后,打开的将会是发短信页面 ,如下:

这是因为构造Intent时
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
指明了该Intent的动作是 ACTION_SENDTO 即发短信动作
数据 uri为 Uri uri = Uri.parse("smsto:10010");
intent.putExtra("sms_body", "This is the sms text");
并设置了短信内容为 "This is the sms text"
所以点击Button调用startActivity(intent)方法后看到的会是上面的发短信界面

.心得

到现在才算真正开始入门Android,以后笔记不会记得像以前那么太详细,把重点难点记住就可以,也不用花太多时间
发现自己英语水平真的很差,Android的帮助文档内容很多看不懂的 尴尬



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值