笔记-Intent跳转

来源:互联网 发布:2016京东618销售数据 编辑:程序博客网 时间:2024/05/16 09:47

一、显式跳转

MainActity.java

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //设置Button监听        findViewById(R.id.btyStarNewActivity).setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                startActivity(new Intent(MainActivity.this, aativity.class));//跳转到 aactivity            }        });    }}

activity_main.xml 布局

选择线性布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:id="@+id/btyStarNewActivity"        android:text="启动一个Activity"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

接下来手动创建一个activity 命名为aactivity.xml

aactivity.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:text="这是另一个Activity"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="40dp"/></LinearLayout>

由于这里是手动添加的activity,所以需要在 AndroidMainifest.xml中手动添加入<activity android:name=".aativity"/>

AndroidMainifest.xml


<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="news.com.study_intent">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity            android:name=".MainActivity"            android:label="@string/app_name"            android:theme="@style/AppTheme.NoActionBar">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".aativity"/>    </application></manifest>


aactivity.java

import android.app.Activity;import android.os.Bundle;public class aativity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.aativity);    }}


二、隐式跳转

1、直接传入字符串

AndroidMainifest.xml中设置

            <intent-filter>                <category android:name="android.intent.category.DEFAULT"/>                <!--指明是默认的category,一般在隐式地启动activity时需要用到category            [http://blog.csdn.net/ygc87/article/details/7480695]-->                <action android:name="asafdd" />                <!--这里的字符串用来传值-->            </intent-filter>

AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="news.com.study_intent">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity            android:name=".MainActivity"            android:label="@string/app_name"            android:theme="@style/AppTheme.NoActionBar">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".aativity">            <intent-filter>                <category android:name="android.intent.category.DEFAULT"/>                <action android:name="asafdd" />            </intent-filter>        </activity>    </application></manifest>


MainActivity.java

package news.com.study_intent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //设置Button监听        findViewById(R.id.btyStarNewActivity).setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                startActivity(new Intent("asafdd"));//跳转到 aactivity            }        });    }}

使用这种方法可以实现 启动另一个APP中的Activity
http://blog.csdn.net/lincyang/article/details/45503675

0 0
原创粉丝点击