Archives
Recent Posts
«   2025/02   »
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
Today
Total
관리 메뉴

안드로이드 개발자의 창고

[30일차 Android] Bar(ProgressBar, SeekBar, RatingBar) 본문

Computer/Android

[30일차 Android] Bar(ProgressBar, SeekBar, RatingBar)

Wise-99 2023. 6. 14. 12:52

 

 

 

출처 : 안드로이드 앱스쿨 2기 윤재성 강사님 수업 PPT

 

 

 

📖 ProgressBar

  • 오래 걸리는 작업이 있을 경우 작업 중임을 표시하는 View

주요 속성

속성 설명
style ProgressBar의 모양을 설정
max 최대 값
progress 현재 값

주요 프로퍼티

프로퍼티 설명
progress 현재 값을 관리

주요 메서드

메서드 설명
incrementProgressBy 지정한 값 만큼 증가 혹은 감소시킨다.

📖 SeekBar

  • ProgressBar와 매우 유사하지만 사용자가 값을 직접 지정할 수 있는 기능을 갖추고 있다.

주요 속성

속성 설명
style SeekBar의 모양을 설정
max 최대 값
progress 현재 값

 

주요 프로퍼티

프로퍼티 설명
progress 현재 값을 관리

주요 메서드

메서드 설명
incrementProgressBy 지정한 값 만큼 증가 혹은 감소시킨다.

주요 이벤트

이벤트 설명
SeekBarChange SeekBar의 상태가 변경되었을 때

📖 RatingBar

  • 별점을 조절할 수 있는 View

주요 속성

속성 설명
numStars 별의 개수를 설정
stepSize 별점이 조절되는 양을 설정
rating 현재 별점을 설정
isIndicator 별점을 사용자가 조절 할 수 있는지 설정. true를 설정하면 사용자는 별 점을 조절 할 수 없다.
style 스타일을 설정
progressDrawable 별의 이미지를 설정

주요 프로퍼티

프로퍼티 설명
rating 별점을 관리

주요 이벤트

이벤트 설명
RatingBarChange 별 점이 변경되었을 때

📖 예제 코드

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" >

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />

    <SeekBar
        android:id="@+id/seekBar2"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isIndicator="false"
        android:numStars="7"
        android:rating="3.5"
        android:stepSize="0.5" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

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

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

</LinearLayout>

 

 

 

MainActivity.kt

package com.test.android24_bar

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import com.test.android24_bar.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    lateinit var activityMainBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(activityMainBinding.root)

        activityMainBinding.run {
            button.run {
                setOnClickListener {
                    // ProgressBar의 값을 설정
                    progressBar2.progress = 70
                    // SeekBar의 값을 설정
                    seekBar.progress = 70
                    seekBar2.progress = 70
                    //RatingBar의 값을 설정
                    ratingBar.rating = 1.5F
                }
            }

            button2.run {
                setOnClickListener {
                    // SeekBar에 설정된 값을 가져와 출력
                    textView.text = "SeekBar1 : ${seekBar.progress}\n"
                    textView.append("SeekBar2 : ${seekBar2.progress}\n")
                }
            }

            seekBar.run {
                setOnSeekBarChangeListener(object : OnSeekBarChangeListener{
                    // progress : 새롭게 설정된 값
                    // fromUser : 사용자가 변경한 것인지 여부
                    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                        textView2.text = "${progress}\n"
                        if(fromUser){
                            textView2.append("사용자에 의해 변경되었습니다\n")
                        } else {
                            textView2.append("코드를 통해 변경되었습니다\n")
                        }
                    }

                    override fun onStartTrackingTouch(p0: SeekBar?) {
                        TODO("Not yet implemented")
                    }

                    override fun onStopTrackingTouch(p0: SeekBar?) {
                        TODO("Not yet implemented")
                    }

                })
            }

            ratingBar.run{
                // rating : 설정된 별점 값
                // fromUser : 사용자에 의해 설정되었는지 여부
                setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
                    textView2.text = "Rating : ${rating}\n"
                    if(fromUser){
                        textView2.append("사용자에 의해 변경되었습니다\n")
                    } else {
                        textView2.append("코드를 통해 변경되었습니다\n")
                    }
                }
            }
        }
    }
}

'Computer > Android' 카테고리의 다른 글

[31일차 Android] FloatingActionButton  (0) 2023.06.14
[31일차 Android] CardView  (0) 2023.06.14
[30일차 Android] Chip  (0) 2023.06.14
[30일차 Android] CheckedTextView  (0) 2023.06.14
[30일차 Android] ToggleButton과 Switch  (0) 2023.06.14