ASウェブページ編②問題~Intentを使って任意のURLのウェブページを開く~

MainActivity.java

package com.example.webappli;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
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 webBtn = findViewById(R.id.webBtn);
webBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
startActivity(intent);
}
});
}
}

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center_horizontal">

<Button
android:id="@+id/webBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ウェブページを開く"
android:layout_marginTop="50dp"/>

</LinearLayout>

これだけで基本開けます。

 

暗黙的インテントの学習が有用です。

下記参考

Android の基礎 02.3: 暗黙的な意図