关于 Activity 的跳转。

最近有看Activity ,好多不懂,连个简单的跳转都不能顺利完成,结果当然,定实现个!哈

Activity 的跳转需要一个触发,不可能无缘无故的跳转,可以是最简单的 Button 触发,也可以其他的 OnClickListener(),各种跳转方法,这就需要大家去发掘,

找到了给 CC 一份啊!

跳转之一,直接设置 Intent 。

Intent intent = new Intent();
intent.setClass(this,xx.class); //其中第一个参数代表自身,在触发事件中,直接写 this 会出错,一般都是写类名.this。第二个参数就是跳转目标类。
startActivity(intent); // 开始跳转

跳转之二,给 Intent 设置 action 。

下面做的简单 demo 。

package com.gao;

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

public class StartActivity extends Activity {
	
	private Button mButton1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mButton1 = (Button) findViewById(R.id.button_show);
        mButton1.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("gao", " hello gao  "); // 要传递的参数
				Intent intent = new Intent();
				intent.putExtras(bundle);
				intent.setAction("com.gao.another.BundleTestAc"); // 设置 action 
				startActivity(intent);
				
			}
        	
        });
        
        
    }
}


上面的跳转要成功,还有个关键,那就是需要在 AndroidManifest.xml 中设置自身,相当于注册,要不别人不认识。

	<activity android:name=".another.BundleTestAc"> // 这个是类自身
        	<intent-filter>
        		<action android:name="com.gao.another.BundleTestAc"></action> //如果用到 action 跳转,这个必须设置。
        		<category android:name="android.intent.category.DEFAULT" /> // 用到 intent-filter ,那这个属性也必须设置。
        	</intent-filter>
        </activity>

就写这么多,我这个懒人不太喜欢深究,好多原理都是不懂,哎....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值