티스토리 뷰

Android Projects

전화번호부 만들기

IT Knowledge Share 2021. 7. 9. 14:06
반응형

전화번호부 어플을 만드는 연습을 해보겠습니다.

전화번호부 목록이 나오고, 해당 목록의 아이템을 들어가면, 아이템에 이름과 전화번호가 나오도록 하는 기능입니다.

 

1. 먼저, Phonebook 액티비티 하나를 만들어줍니다.

2. 해당 액티비티에 Phonebook( ), Person( ) 클래스를 만듭니다.

3. Phonebook( ) 클래스 안에 addPerson 함수를 만듭니다.

4. personList에 Person( ) 클래스의 인자들이 들어갈 수 있도록 선언해둡니다.

5. phoneBook.personList의 아이템을 하나씩 담을 레이아웃 리소스 파일을 만듭니다.

6. Phonebook 액티비티의 XML로 가서 컨테이너를 만듭니다.

7. 넘어갈 상세페이지 액티비티를 만듭니다.

 

1. activity_phonebook.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=".PhonebookActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:gravity="center"
        android:padding="4dp"
        android:text="김형용 전화번호부" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:gravity="center"
        android:padding="4dp"
        android:text="Hyungyong Kim's PhoneBook" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/phonebook_list_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"/>

    </ScrollView>

</LinearLayout>
반응형

2. PhonebookActivity.kt

package com.example.myapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView

class PhonebookActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_phonebook)

        val phoneBook = createFakeNumber(30)
        createPhonebookList(phoneBook)
    }

    fun createFakeNumber(fakeNumber: Int = 10, phoneBook: PhoneBook = PhoneBook()): PhoneBook{
        for (i in 0 until fakeNumber){
            phoneBook.addPerson(
                Person(
                    name = "" + i + "th Person",
                    number = "" + i + "th Person's Number"
                )
            )

        }

        return phoneBook
    }

    fun createPhonebookList(phoneBook: PhoneBook){
        val layoutInflater = LayoutInflater.from(this@PhonebookActivity)
        val container = findViewById<LinearLayout>(R.id.phonebook_list_container)
        for (i in 0 until phoneBook.personList.size){
            val view = layoutInflater.inflate(R.layout.phonebook_item, null)
            val personNameView = view.findViewById<TextView>(R.id.person_name)
            personNameView.setText(phoneBook.personList.get(i).name)
            addSetOnClickListener(phoneBook.personList.get(i), view)
            container.addView(view)
        }
    }

    fun addSetOnClickListener(person: Person, view: View){
        view.setOnClickListener {
            val intent = Intent(this@PhonebookActivity, PhonebookDetailActivity::class.java)
            intent.putExtra("name", person.name)
            intent.putExtra("number", person.number)
            startActivity(intent)
        }

    }


}

class PhoneBook(){
    val personList = ArrayList<Person>()
    fun addPerson(person: Person){
        personList.add(person)
    }

}

class Person(val name: String, var number: String){

}

 

3. activity_phonebook_detail.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"
    android:background="#673AB7"
    tools:context=".PhonebookDetailActivity">

    <TextView
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go Back"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:background="#0C0A0A"
        android:textSize="20dp"
        android:layout_marginBottom="16dp"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/solid"/>

    <TextView
        android:id="@+id/person_detail_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sample Name"
        android:textColor="#ffffff"
        android:textSize="50dp"
        android:gravity="center"
        android:layout_marginBottom="16dp"/>

    <TextView
        android:id="@+id/person_detail_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="000-0000-0000"
        android:textColor="#ffffff"
        android:textSize="50dp"
        android:gravity="center"
        android:layout_marginBottom="16dp"/>


</LinearLayout>

4. PhonebookDetailActivity.kt

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_phonebook_detail.*

class PhonebookDetailActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_phonebook_detail)

        getPersonInfoAndDraw()

        //뒤로가기 버튼
        back.setOnClickListener {
            onBackPressed()
        }
    }

    //인텐트 받는쪽
    fun getPersonInfoAndDraw(){
        val name = intent.getStringExtra("name")
        val number = intent.getStringExtra("number")

        //정보 받은 후 뷰 그려주기
        person_detail_name.setText(name)
        person_detail_number.setText(number)
    }

}

5. phonebook_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#121111"
    android:gravity="center_vertical"
    android:padding="6dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginRight="16dp"
            android:src="@drawable/stroke"/>

        <TextView
            android:id="@+id/person_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="20dp"
            android:text="사용자"
            android:textStyle="bold"
            android:gravity="center"
            android:textColor="#ffffff"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ffffff"/>

</LinearLayout>

 

반응형
댓글