Activity中Intent的使用

Intent是android中很重要的一个机制,可以多个Activity中间传递消息(也可以在Server中传递消息,本文不涉及)。列举两个例子:

一、通过ActivityA启动ActivityB:

1、首先建立工程,名为IntentText,并带有一个名ActivityA的Activity.

2、更改ActivityA的配置文件main.xml,添加一个按钮:

main.xml:

<?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/myBtn"
        android:text="@string/button_Text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
 />

</LinearLayout>
注意要在string.xml中增加button_Text字段;
3、添加一个继承自Activity的类ActivityB,并重写它的OnCreate函数。建立一个mainb.xml为其配置文件,在AndroidManifest.xml中注册ActivityB.

mainb.xml:

<?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" >
</LinearLayout>
ActivityB.java:

package com.IntentTest;

import android.app.Activity;
import android.os.Bundle;

public class ActivityB extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainb);
	}
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.IntentTest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ActivityA"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActivityB"
            android:label="@string/app_name" >
        </activity>
    </application>
</manifest>


4、在ActivityA中的OnCreate中加入监听借口类,并在其中new一个intent。将监听类绑定到按钮上:

ActivityA.java:

package com.IntentTest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

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

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(ActivityA.this, ActivityB.class);
			ActivityA.this.startActivity(intent);
		}	
    }
}

效果


未完待续




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值