동작 과정
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"
android:layout_marginTop="200dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"
android:layout_marginTop="200dp"
/>
</LinearLayout>
JAVA
package com.example.a2_57;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 버튼 객체를 XML 파일에서 찾아와서 button 변수에 할당합니다.
button = (Button) findViewById(R.id.button1);
// 버튼에 클릭 이벤트 리스너를 설정합니다.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 전화를 걸기 위한 Intent를 생성합니다. ACTION_VIEW를 사용하여 전화를 보여줍니다.
// Uri.parse("tel:010-1234-5678")를 사용하여 전화번호를 파싱하고 Intent에 추가합니다.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:010-1234-5678"));
// 생성한 Intent를 시작하여 전화를 걸도록 합니다.
startActivity(intent);
}
});
}
}