짱짱해커가 되고 싶은 나

02-1. 여러 기능을 하는 버튼 애플리케이션 구현 본문

모바일

02-1. 여러 기능을 하는 버튼 애플리케이션 구현

동로시 2021. 1. 23. 00:12

4가지 버튼이 있는 앱을 구현할 계획이다.

- 네이버 사이트 열기

- 119 걸기

- 갤러리 열기

- 앱 종료하기

 

> 버튼 생성 (activity_main.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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/strBtn1">
    </Button>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/strBtn2">
    </Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="@string/strBtn3">
    </Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:text="@string/strBtn4">
    </Button>

</LinearLayout>

LinearLayout을 사용했는데 이 레이아웃은 뷰를 한 쪽 방향으로 쌓는다. ex) 가로, 세로

나는 버튼을 한 줄로 세울거기 때문에 orientation속성을 vertical로 설정했다.

그리고 각 button의 id를 지정하고 버튼의 문자열은 strings.xml에 저장되어있다.

 

> 버튼 문자열 생성 (strings.xml)

<resources>
    <string name="app_name">BaseApp</string>
    <string name="strBtn1">네이버 홈페이지 열기</string>
    <string name="strBtn2">119 응급전화 걸기</string>
    <string name="strBtn3">갤러리 열기</string>
    <string name="strBtn4">종료</string>
</resources>

 

> 버튼 이벤트 지정 (MainActivity.java)

package com.example.baseapp;

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 button1, button2, button3, button4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById((R.id.button3));
        button4 = (Button)findViewById((R.id.button4));

        button1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.naver.com"));
                startActivity(intent);
            }
        });

        button2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:/119"));
                startActivity(intent);
            }
        });

        button3.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
               Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
               startActivity(intent);
            }
        });

        button4.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                finish();
            }
        });

    }
}

'모바일' 카테고리의 다른 글

02-3. BaseApp2 구현  (0) 2021.02.15
02-2. Android 프로젝트 구성  (0) 2021.01.28
02. 안드로이드 프로젝트 기본 틀  (0) 2021.01.22
01. HelloAndroid  (1) 2021.01.22
00. 안드로이드 개요  (0) 2021.01.04
Comments