Android——Activity跳转

Posted Chen_s

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android——Activity跳转相关的知识,希望对你有一定的参考价值。

技术分享

目的:实现Activity之间的跳转

技术分享

技术分享

技术分享

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.chenshuai.excise.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!首页"
        android:textSize="40dp"
        android:id="@+id/textView" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="65dp"
        android:text="跳转至下一页"
        android:textSize="40dp"
        android:id="@+id/clickec"
        android:onClick="clickec"/>



</RelativeLayout>

MainActivity.java
package com.example.chenshuai.excise;

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);
    }

public void clickec(View v)
{
    //先创建意图  Intent
    //第一个参数 来源实例,就是当前Activity实例
    //第二个参数 目标类,目标Activity的class
    Intent in = new Intent(this,excise1.class);
    //发动意图
    startActivity(in);

}




}

需要注意的是这个方法必须符合三个条件:

  1.public

  2.返回void

  3.只有一个参数View,这个View就是被点击的这个控件。

 

excise1.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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Excise1 第二页"
        android:textSize="40dp"
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="返回"
        android:textSize="40dp"
        android:id="@+id/clickec1"
        android:onClick="clickec1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300px"
        android:text="跳转至第三页"
        android:textSize="40dp"
        android:id="@+id/clickec2"
        android:onClick="clickec2"/>


</LinearLayout>

excise1.java

package com.example.chenshuai.excise;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

/**
 * Created by chenshuai on 2016/3/20.
 */
public class excise1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.excise1);
        Log.e("tag","创建");
    }

 /*   @Override
    protected void onStart() {
        super.onStart();
        Log.e("tag","启动");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("tag","显示");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("tag","暂停");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("tag","停止");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("tag","重新启动");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("tag","销毁");
    }*/
    public void clickec1(View v)
    {

        Intent in =new Intent(this,MainActivity.class);
        startActivity(in);
    }
    public void clickec2(View v)
    {
        Intent in1 = new Intent(this,excise2.class);
        startActivity(in1);
    }
}

excise2.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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="excise2 第三页"
        android:textSize="40dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="返回"
        android:textSize="40dp"
        android:id="@+id/clickec3"
        android:onClick="clickec3"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="250px"
        android:text="关闭"
        android:textSize="40dp"
        android:id="@+id/clickec4"
        android:onClick="clickec4"/>

</LinearLayout>

excise2.java

package com.example.chenshuai.excise;

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

/**
 * Created by chenshuai on 2016/3/20.
 */
public class excise2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.excise2);
    }
    public void clickec3(View v)
    {
        Intent in = new Intent(this,excise1.class);
        startActivity(in);
    }
    public void clickec4(View v)
    {
        //关闭
        finish();
    }

}

最后别忘了在AndroidManifest.xml里注册

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chenshuai.excise">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".excise2">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name=".excise1">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>




    </application>

</manifest>

 

 


 

 

 

以上是关于Android——Activity跳转的主要内容,如果未能解决你的问题,请参考以下文章

Android从Fragment跳转到Activity

android:startActivityForResult方法的Activity跳转

android学习——实现两个activity之间的跳转

Android-Activity跳转时动画

Android中实现activity的页面跳转并传值

android开发问题:如何在当前Acitivity中用代码获取上一个Activity名称?