1. 程式人生 > >Android stdio 學習之 Intent 類(啟動外部瀏覽器或實現頁面元件跳轉)

Android stdio 學習之 Intent 類(啟動外部瀏覽器或實現頁面元件跳轉)

首先

1.確保已經安裝好了Android stdio 環境 

2.瞭解Intent類是什麼

詳情可檢視網址:https://developer.android.com/guide/components/intents-filters

說白了,Intent就是一個捆綁各個元件的紐帶,不僅僅可以聯絡捆綁自己應用本身的元件,同樣也可以聯絡別的應用程式的元件。

比如說,我們常用的微信中的掃一掃功能,微信本身並不帶有拍照功能,但是它可以呼叫系統中的相關元件,讓它看起來微信自身是帶有照相模組的。

3.開始實現Intent的簡單外部呼叫

通過簡單的幾行程式碼,實現在本應用中啟動瀏覽器或者其他相關應用

這裡我們選擇點選按鈕實現訪問外部瀏覽器並導航到百度首頁,當然,我們同樣也可以在本應用中巢狀一個網頁什麼的,但不在本文涉及內容。

一,建立一個空的專案

二,在開啟layout中的佈局檔案和MainActivity.java檔案(預設開啟)

三,在佈局檔案中新增一個Button並進行相關設定

四,在onCreate函式內建立一個Button類變數並與Button(佈局Button的Id進行捆綁)

五,宣告一個Intent 類變數,前提是已經設定好了網址,並用Uri.parse解析網址

六,開始活動

詳細程式碼如下: MainActivity.java

package com.example.administrator.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String baidu = "http://www.baidu.com";  // 網址
                Uri webdress = Uri.parse(baidu);  // 解析網址
                Intent intent = new Intent(Intent.ACTION_VIEW, webdress); // 建立繫結
                startActivity(intent); // 開始活動
            }
        });

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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.25" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.098" />

</android.support.constraint.ConstraintLayout>

其中包的名稱可能不同,只需要換成自己的包名即可。

在者 其實頁面跳轉也很簡單,確保自己已經新建了一個MainActivity(作為主介面),在app檢視上右鍵新建一個empty activity作為第二頁面,這時,我們就有了兩個活動,兩個xml佈局檔案,當然活動,佈局的名稱是不同的。

這裡只簡單說一下如何實現跳轉,與上文的情況基本一致,詳情看程式碼。

MainActivity.java 這裡我引用了我之間做的一個小程式的介面

package com.example.administrator.simplecalculate;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button simplebtn = (Button) findViewById(R.id.simplebtn);    // 繫結button 的 id
        simplebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MainActivity.this,simple.class);//要跳轉的活動(這個在主函式,所以就是MainActivity.this,simple.class是我另一個activity)
                startActivity(intent1);    // 開始跳轉
            }
        });    //基本上就這三個主要部分,當然我們也可以採取更詳細但比較麻煩的步驟
                // intent.setAction() intent.setData() 來使用

        Button two_nbtn = (Button) findViewById(R.id.two_nbtn);
        two_nbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(MainActivity.this,two_n.class);
                startActivity(intent2);
            }
        });

        Button three_nbtn = (Button) findViewById(R.id.three_nbtn);
        three_nbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3 = new Intent(MainActivity.this,three_n.class);
                startActivity(intent3);
            }
        });

    }
}

佈局檔案

<?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/simplebtn"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="簡單計算"
        app:layout_constraintBottom_toTopOf="@+id/two_nbtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/two_nbtn"
        android:layout_width="200dp"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="二階行列式計算"
        app:layout_constraintBottom_toTopOf="@+id/three_nbtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/three_nbtn"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="172dp"
        android:text="三階行列式計算"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/hint"
        android:layout_width="78dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="選擇功能:"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.113"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.115" />
</android.support.constraint.ConstraintLayout>