# Android Programming - Lecture 3 ###### tags: `Kotlin Programming` `Android Programming` ## Intent * 可用 Intent 去叫起其他 Activity,並傳遞資料。 * 可用 Implicit Intent 可以開啟網頁、開啟地圖、分享資料等。 ### 叫起其他 Activity 首先我們開啟一個新的空專案,並且練習如何建立`Activity`,使用`Intent`切換與帶資料。 #### 建立專案 1. Create New Project。  2. Template 選擇 Empty Activity。  3. 命名為 IntentPractice1,語言選擇 kotlin ,Minimum SDK 選擇 Android 11.0。  #### 建立新的 Activity 對著 com.example.intenttest 按`右鍵`->`New`->`Activity`->`Empty Activity`。  Activity Name 命名為:ChildActivity,Layout Name命名為:activity_child。  完成後可看見多了程式中`ChildActivity`與 Layout 中多了`activity_child.xml`。  AndroidManifest.xml中也多出了`ChildActivty`的宣告。 ```xml= <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.intentpractice1"> <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/Theme.IntentPractice1"> <!-- ------------------這裡開始------------------ --> <activity android:name=".ChildActivity"></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> ``` #### Layout ##### activity_main.xml 我們在 Main Activity 中建立文字輸入窗與按鈕,並把輸入的內容在按鈕`onClick`時帶到 Child Activity。 ```xml= <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:paddingLeft="16dp" android:paddingTop="16dp" android:paddingRight="16dp" android:paddingBottom="16dp" tools:context=".MainActivity"> <EditText android:id="@+id/et_text_entry" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt_switch_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch Activity" /> </LinearLayout> ``` ##### activity_child.xml 在 Child Activity 中建立文字顯示區塊,並把 MainActivty 傳遞過來的資料顯示出來。 ```xml= <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:paddingLeft="16dp" android:paddingTop="16dp" android:paddingRight="16dp" android:paddingBottom="16dp" tools:context=".MainActivity"> <TextView android:id="@+id/tv_display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Default Text" /> </LinearLayout> ``` #### Gradle 設定 使用 View Binding 前必須先在 build.gradle 中設定 viewBinding 爲 true。 ``` android { ... buildFeatures { viewBinding true } } ``` #### 程式 ##### MainActivity.kt 在 MainActivty 取得文字方塊輸入,並建立Intent,帶入 ChildActivity 與資料,並開起ChildActivity。 ```kotlin= val textEntered = binding.etTextEntry.text.toString() Intent(this, ChildActivity::class.java).run { putExtra(Intent.EXTRA_TEXT, textEntered) }.let { startActivity(it) } ``` 完整 MainActivity.kt: ```kotlin= package com.example.intentpractice1 import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.example.intentpractice1.databinding.ActivityMainBinding private lateinit var binding: ActivityMainBinding class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) binding.btSwitchActivity.setOnClickListener { val textEntered = binding.etTextEntry.text.toString() Intent(this, ChildActivity::class.java).run { putExtra(Intent.EXTRA_TEXT, textEntered) }.let { startActivity(it) } } } } ``` ##### ChildActivity.kt 在 ChildActivity 中取得 Intent 帶過來的資料,並設定在 TextView 中。 ```kotlin= intent.let { if (it.hasExtra(Intent.EXTRA_TEXT)) { binding.tvDisplay.text = it.getStringExtra(Intent.EXTRA_TEXT) } } ``` 完整 ChildActivity.kt: ```kotlin= package com.example.intentpractice1 import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.example.intentpractice1.databinding.ActivityChildBinding private lateinit var binding: ActivityChildBinding class ChildActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityChildBinding.inflate(layoutInflater) setContentView(binding.root) intent.let { if (it.hasExtra(Intent.EXTRA_TEXT)) { binding.tvDisplay.text = it.getStringExtra(Intent.EXTRA_TEXT) } } } } ``` #### 執行 最後可以達到在MainActivity輸入文字並按下`SWITCH ACTIVITY`按鈕後,跳至ChildActivity並顯示對應內容。  ### 開啟網頁、開啟地圖、分享資料。 接著我們開啟一個新的空專案,並且練習如何使用 Intent 開啟網頁、開啟地圖、分享資料等。 其他 Intent 的使用可以參考:https://developer.android.com/guide/components/intents-common #### 建立專案 1. Create New Project。  2. Template 選擇 Empty Activity。  3. 命名為 IntentPractice2,語言選擇 kotlin ,Minimum SDK 選擇 Android 11.0。  #### Layout 我們在 Main Activity 中建三個按鈕按鈕,分別是開啟網頁、開啟地圖、分享文字。 activty_main.xml ```xml= <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:padding="32dp" tools:context=".MainActivity"> <Button android:id="@+id/bt_open_webpage" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="開啟網頁" /> <Button android:id="@+id/bt_open_map" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="開啟地圖" /> <Button android:id="@+id/bt_share_text" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="分享文字" /> </LinearLayout> ``` #### Gradle 設定 使用 View Binding 前必須先在 build.gradle 中設定 `viewBinding` 爲 `true`。 ``` android { ... buildFeatures { viewBinding true } } ``` #### 程式 * 開啟瀏覽器 ```kotlin= binding.btOpenWebpage.setOnClickListener { val web: Uri = Uri.parse("https://www.nctu.edu.tw") startActivity(Intent(Intent.ACTION_VIEW, web)) } ``` * 開啟地圖 ```kotlin= binding.btOpenMap.setOnClickListener { val addressString = "新竹市東區大學路1001號" Intent(Intent.ACTION_VIEW).apply { data = Uri.Builder().apply { scheme("geo") path("0,0") appendQueryParameter("q", addressString) }.build() }.let { startActivity(it) } } ``` * 分享文字 ```kotlin= binding.btShareText.setOnClickListener { val textString = "國立交通大學" ShareCompat.IntentBuilder.from(this).setType("text/plane").setText(textString).startChooser() } ``` 完整 MainActivity.kt: ```kotlin= package com.example.intentpractice2 import android.content.Intent import android.net.Uri import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ShareCompat import com.example.intentpractice2.databinding.ActivityMainBinding import kotlin.math.min private lateinit var binding: ActivityMainBinding class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) binding.btOpenWebpage.setOnClickListener { val web: Uri = Uri.parse("https://www.nctu.edu.tw") startActivity(Intent(Intent.ACTION_VIEW, web)) } binding.btOpenMap.setOnClickListener { val addressString = "新竹市東區大學路1001號" Intent(Intent.ACTION_VIEW).apply { data = Uri.Builder().apply { scheme("geo") path("0,0") appendQueryParameter("q", addressString) }.build() }.let { startActivity(it) } } binding.btShareText.setOnClickListener { val textString = "國立交通大學" ShareCompat.IntentBuilder.from(this).setType("text/plane").setText(textString).startChooser() } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up