关于 Activity 的跳转。

来源:互联网 发布:sql不同数据类型运算 编辑:程序博客网 时间:2024/05/21 19:36

最近有看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(){@Overridepublic 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>

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

原创粉丝点击