반응형
완성된 UI
proejct 설명
4개의 버튼을 만들고 해당 버튼을 클릭했을 때 버튼마다 다른 기능을 부여한다.
구현할 기능
1. 홈페이지 열기
2. 전화 걸기
3. 갤러리 열기
4. 애플리케이션 종료
activity_main.xml 파일
<?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:orientation="vertical" >
<Button
android:id="@+id/btnGoogle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C8C8FF"
android:layout_weight="1"
android:textSize="20sp"
android:textStyle="bold"
android:text="구글 홈페이지 열기" />
<Button
android:id="@+id/btn112"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8C8CFF"
android:layout_weight="1"
android:textSize="20sp"
android:textStyle="bold"
android:text="112 신고 전화 걸기" />
<Button
android:id="@+id/btnPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7878FF"
android:layout_weight="1"
android:textSize="20sp"
android:textStyle="bold"
android:text="갤러리 열기" />
<Button
android:id="@+id/btnEnd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#5A5AFF"
android:layout_weight="1"
android:textSize="20sp"
android:textStyle="bold"
android:text="애플리케이션 종료" />
</LinearLayout>
MainActivity.java 파일
package com.TIL.myapplication;
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 btnGoogle;
Button btn112;
Button btnPhoto;
Button btnEnd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle(" 버튼 별 기능");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.click);
btnGoogle = (Button) findViewById(R.id.btnGoogle);
btn112 = (Button) findViewById(R.id.btn112);
btnPhoto = (Button) findViewById(R.id.btnPhoto);
btnEnd = (Button) findViewById(R.id.btnEnd);
btnGoogle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.google.com"));
startActivity(mIntent);
}
});
btn112.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:/112"));
startActivity(mIntent);
}
});
btnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
startActivity(mIntent);
}
});
btnEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
새로 알게 된 부분
홈페이지 열기
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.google.com"));
startActivity(mIntent);
전화 다이얼 열기
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:/112"));
startActivity(mIntent);
갤러리 열기
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
startActivity(mIntent);
댓글