본문으로 바로가기

감성 자동제어 

[안드로이드 스튜디오]버튼클릭시 홈페이지 열자!

안녕하세요! 오늘은 버튼을 눌렀을 때 인텐트를 이용해서 네이버 홈페이지를 띄우는 방법에 대해서 알아보도록 하겠습니다! 주소는 얼마든지 변경이 가능하니 원하시는 url 주소를 입력하시면 됩니다!

 

Activity_main.xml(layout)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <Button
        android:id="@+id/btn_url"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="홈페이지 띄우기"
        android:textSize="40dp" />
 
</LinearLayout>
cs

 

MainActivity(Class)

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
28
29
30
31
package com.example.toast_message;
 
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 {
 
    Button mbtn_url;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mbtn_url = findViewById(R.id.btn_url);
 
        mbtn_url.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent urlintent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com"));
                startActivity(urlintent);
            }
        });
 
    }
}
cs

 

마무리

이상으로 버튼클릭시 홈페이지를 띄우는 방법에 대해서 알아보았습니다. 만약 다른 홈페이지를 띄우고 싶다면 메인 액티비티의 노런 글씨의("http://m.naver.com") url을 수정해주시면 간단하게 변경됩니다! 감사합니다!

 

 

결과화면

 

긴 글 읽느라 수고하셨습니다.

오늘도 일상 속 소소한 행복을 느끼길 바랍니다!

 

더 많은 정보

https://engineering-mino.tistory.com/