実現したいこと
ImageButtonを押してアプリを起動できるようにする。(今回はRakuten Link)
環境
Android Studio Bumblebee | 2021.1.1 Patch 2
Kotlin Plugin Version 211-1.6.10-release-923-AS7442.40
Android 9.0
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="horizontal"> 13 14 <LinearLayout 15 android:layout_width="match_parent" 16 android:layout_height="match_parent" 17 android:orientation="vertical"> 18 19 <ImageButton 20 android:id="@+id/link" 21 android:layout_width="150dp" 22 android:layout_height="150dp" 23 android:layout_marginLeft="12dp" 24 android:layout_marginTop="12dp" 25 android:onClick="link" 26 app:srcCompat="@drawable/icon_link" 27 tools:ignore="SpeakableTextPresentCheck" /> 28 29 <TextView 30 android:id="@+id/textView" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:layout_marginLeft="64dp" 34 android:text="@string/rakuten_link" 35 android:textAlignment="center" 36 android:textSize="20sp" 37 android:textStyle="bold" /> 38 </LinearLayout> 39 40 </LinearLayout> 41</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
1package com.hamachi.rmini_home 2 3import android.content.Intent 4import android.net.Uri 5import androidx.appcompat.app.AppCompatActivity 6import android.os.Bundle 7import android.widget.Button 8import android.widget.ImageButton 9 10class MainActivity : AppCompatActivity() { 11 override fun onCreate(savedInstanceState: Bundle?) { 12 super.onCreate(savedInstanceState) 13 setContentView(R.layout.activity_main) 14 15 val button = findViewById<ImageButton>(R.id.link) 16 button.setOnClickListener { 17 18 val intent = Intent(Intent.ACTION_VIEW) 19 intent.setPackage("jp.co.rakuten.mobile.rcs") 20 startActivity(intent) 21 } 22 } 23}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/04 08:11