AndroidStudio (kotlin) 검색 바에서 문자 크기 변경

10473 단어 AndroidStudioKotlin

하고 싶은 일


  • 화면에 시간 표시 (디지털 시계)
  • 탐색 바 표시
  • 탐색 바를 움직이면 시계의 문자 크기가 변경됩니다



  • 구현



    화면


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ここに時計が表示される"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <SeekBar
            android:id="@+id/skb"
            android:layout_width="304dp"
            android:layout_height="71dp"
            android:layout_marginBottom="24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    메인 처리


    package com.example.yamato200605c
    
    import android.icu.util.Calendar
    import android.os.Build
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.os.Handler
    import android.widget.SeekBar
    import androidx.annotation.RequiresApi
    import kotlinx.android.synthetic.main.activity_main.*
    import kotlin.concurrent.timer
    
    class MainActivity : AppCompatActivity() {
    
        @RequiresApi(Build.VERSION_CODES.O)
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            setupSeekBar()
            handler()
        }
    
        @RequiresApi(Build.VERSION_CODES.O)
        private fun setupSeekBar()
        {
            skb.progress = 30
            skb.min = 10
            skb.max = 50
            skb.setOnSeekBarChangeListener(
                    object : SeekBar.OnSeekBarChangeListener {
                        override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                            textView.textSize = progress.toFloat()
                        }
                        override fun onStartTrackingTouch(seekBar: SeekBar) {
                        }
                        override fun onStopTrackingTouch(seekBar: SeekBar) {
                        }
                    })
        }
    
        @RequiresApi(Build.VERSION_CODES.N)
        private fun handler()
        {
            val handler = Handler()
            timer(name = "testTimer", period = 10) {
                val calendar = Calendar.getInstance()
                val hour = calendar.get(Calendar.HOUR_OF_DAY)
                val minute = calendar.get(Calendar.MINUTE)
                val second = calendar.get(Calendar.SECOND)
                val millisecond = calendar.get(Calendar.MILLISECOND) / 10
                handler.post {
                    textView.text = "${hour}時 ${minute}分 ${second}秒 ${millisecond}"
                }
            }
        }
    }
    

    좋은 웹페이지 즐겨찾기