개발일지

Android in A..Z - Widget 본문

Android (안드로이드)

Android in A..Z - Widget

강태종 2020. 8. 9. 18:33

CalendarView

달력 위젯으로 setOnDateChangeListener를 설정하여 날짜를 선택할 때 이벤트를 발생시킬 수 있다

CalendarView

CalendarFragment.kt

class CalendarFragment : BaseFragment<FragmentCalendarBinding>(R.layout.fragment_calendar) {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)

        binding.fragment = this
        binding.calendar.setOnDateChangeListener { view, year, month, day ->
            Toast.makeText(context, "$year / $month / $day", Toast.LENGTH_SHORT).show()
        }

        return binding.root
    }
}

DatePickerView

init(YEAR, MONTH, DAY, LISTENER)로 초기화 하여 이벤트를 발생시킬 수 있다.

DatePickerView

class DatePickerFragment : BaseFragment<FragmentDatePickerBinding>(R.layout.fragment_date_picker) {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)

        binding.fragment = this
        binding.datePicker.init(2000, 10, 10) { picker, year, month, day ->
            Toast.makeText(context, "$year / $month / $day", Toast.LENGTH_SHORT).show()
        }

        return binding.root
    }
}

Chronometer

초시계 위젯이다. chronometer.base와 SystemClock.elapsedRealtime으로 초기화 할 수 있고, chronometer.start와 chronometer.stop으로 시작/중지를 할 수 있다. 여기서 주의할 점은 stop은 우리에게 보여주는 시간만 멈추는 것이지 실제로 계속 count하고 있다.

Chronometer

class ChronometerFragment : BaseFragment<FragmentChronometerBinding>(R.layout.fragment_chronometer) {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)

        with(binding) {
            fragment = this@ChronometerFragment
            start.setOnClickListener {
                chronometer.base = SystemClock.elapsedRealtime()
                chronometer.start()
            }

            stop.setOnClickListener {
                chronometer.stop()
                chronometer.base = SystemClock.elapsedRealtime()
            }
        }

        return binding.root
    }
}

AutoCompleteTextView

자동완성 기능이 있는 EditText이다. AutoCompleteTextView.setAdapter와 ArrayAdapter로 자동완성을 표시할 수 있다.

AutoCompleteTextView

class AutoEditTextFragment : BaseFragment<FragmentAutoEditTextBinding>(R.layout.fragment_auto_edit_text) {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)

        with(binding) {
            fragment = this@AutoEditTextFragment

            val items = arrayOf("Galaxy S1", "Galaxy S2", "Galaxy S3", "Galaxy Note1", "Galaxy Note2", "Galaxy Note3", "IPhone1", "IPhone2", "IPhone3", "IPod1", "IPod2", "IPod3")
            auto.setAdapter(ArrayAdapter<String>(context!!, android.R.layout.simple_dropdown_item_1line, items))
        }
        return binding.root
    }
}

ProgressBar

Style에 따라 원형바 또는 가로바가 생긴다.

ProgressBar.max로 최대를 설정할 수 있고, ProgressBar.progress로 값을 설정할 수 있다.

ProgressBar No Style

 

ProgressBar Horizontal

class ProgressFragment : BaseFragment<FragmentProgressBinding>(R.layout.fragment_progress) {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        binding.fragment = this
        binding.progress.max = 1000
        return binding.root
    }

    fun onProgress(view: View) {
        with(binding) {
            try {
                progress.progress = editTextNumber.text.toString().toInt()
            }
            catch (e: Exception) {
                Toast.makeText(context, "Invalid Number", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

SeekBar

SeekBar.setOnSeekBarChangeListener로 리스너를 설정하여 이벤트를 발생할 수 있다.

SeekBar

class SeekBarFragment : BaseFragment<FragmentSeekbarBinding>(R.layout.fragment_seekbar) {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        binding.fragment = this

        binding.seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                gage.text = p1.toString()
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }
        })
        return binding.root
    }
}

RatingBar

RatingBar

class RatingFragment : BaseFragment<FragmentRatingBinding>(R.layout.fragment_rating) {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        binding.fragment = this
        binding.rating.onRatingBarChangeListener = RatingBar.OnRatingBarChangeListener { bar, rating, bool ->
            binding.textView.text = rating.toString()
        }
        return binding.root
    }
}

'Android (안드로이드)' 카테고리의 다른 글

Android in A..Z - TextInputLayout  (0) 2021.01.20
Android in A..Z - Glide  (0) 2020.11.07
Android in A..Z - Handler  (0) 2020.07.28
Android in A..Z - Activity Lifecycle  (0) 2020.07.28
Android in A..Z - Notification  (0) 2020.07.27
Comments