กดปุ่มเพื่อเปิด url ที่กำหนดไว้ ด้วยการส่ง intent ออกไปเรียก web browser ของ android
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/bottonGoogle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Google" /> <Button android:id="@+id/bottonPhaisarn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Phaisarn" /> </LinearLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.phaisarn.ktapplication import android.content.Intent import android.net.Uri import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) bottonGoogle.setOnClickListener { val openURL = Intent(Intent.ACTION_VIEW) openURL.data = Uri.parse("https://www.google.com/") startActivity(openURL) } bottonPhaisarn.setOnClickListener { val openURL = Intent(android.content.Intent.ACTION_VIEW) openURL.data = Uri.parse("https://www.phaisarn.com/") startActivity(openURL) } } } |