【Android學習筆記】Intent (1) - 跳轉頁面

來源:初次學習,建議直接看這個官方原版,我這個是總結出來的要點,供自己日後複習,參考

Intent 概念

Intent : An Intent is an asynchronous message that you can use in your activity to request an action from another activity, or from some other app component. You use an intent to start one activity from another activity, and to pass data between activities

An Intent can be explicit or implicit:

  1. 顯示方法 An explicit intent is one in which you know the target of that intent. That is, you already know the fully qualified class name of that specific activity.
  2. 隱示方法 An implicit intent is one in which you do not have the name of the target component, but you have a general action to perform.

這個貼子是顯示

效果圖

點擊跳轉:

步驟

第一步:Create the TwoActivities project (這個就是創建一個新的proj,略過)

第二步: Define the layout for the main Activity

<Button
    android:id="@+id/button_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="28dp"
    android:layout_marginRight="28dp"
    android:layout_marginBottom="36dp"
    android:text="@string/button_main"    這兒有個快捷添加方法見以前的博客
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:onClick="launchSecondActivity"/>  // 產生單擊響應

Button 中添加 添加響應 android:onClick="launchSecondActivity"
添加後會出現 underlined in red ,可以點擊紅色燈,添加Create 'launchSecondActivity(View)' in 'MainActivity.

   public void launchSecondActivity(View view) {
   }

第三步: 添加 Log.d 方便查看

package ...
()
public class MainActivity extends AppCompatActivity {
   private static final String LOG_TAG = 
                               MainActivity.class.getSimpleName(); // private

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }

   public void launchSecondActivity(View view) {
       Log.d(LOG_TAG, "Button clicked!");            // log.d
   }
}

第四步: 創建 第二個Activity

File > New > Activity > Empty Activity.    name =  SecondActivity
The layout name is filled in as activity_second 

這裏官網上面有個不一致,產生的並非是SecondActivity.java 而是 “activity_second”.
The label attribute adds the title of the Activity to the app bar.

        <activity android:name=".activity_second"
            android:label="@string/activity2_name"  //  adds the title of the Activity to the app bar.
            android:parentActivityName=".MainActivity"> // 添加Up navigation 返回父類Activity
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.a1104.twoactivities.MainActivity"/>   // 這個是自己的文件地址,自動提示

        </activity>

第二層的XML設置:
The value of textAppearance is a special Android theme attribute that defines basic font styles.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity_second">

    <TextView
        android:id="@+id/text_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:text="@string/text_header"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_header" />
 
</android.support.constraint.ConstraintLayout>

第五步: Add an Intent to the main Activity

5.1 Create a new Intent in the launchSecondActivity() method in MainActivity

Intent 有兩個參數第一個參數是起始位置,this表當前,第二個參數是需要去的位置 id

Intent intent = new Intent(this,activity_second.class);

5.2 startActivity(intent);啓動

public void launchSecondActivity(View view) {
        Log.d(LOG_TAG,"Button clicked");
        Intent intent = new Intent(this,activity_second.class);
        startActivity(intent);
        Log.d(LOG_TAG, "START SENCOND MESSAGE");

    }

啓動即可


ps:完整代碼:


AndroidManifest.xml

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"
            android:label = "Second Activity"
            android:parentActivityName = ".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.a1104.twoactivities.MainActivity"/>
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/button_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:text="@string/button_main"
        android:onClick="launchSecondActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginEnd="16dp" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.a1104.twoactivities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG =
            MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void launchSecondActivity(View view) {
        Log.d(LOG_TAG, "Button clicked!");
        Intent intent = new Intent(this,SecondActivity.class);
        startActivity(intent);
    }
}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">

    <TextView
        android:id="@+id/text_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:text="@string/text_header"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

SecondActivity.java

package com.example.a1104.twoactivities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章