ストップウォッチ

Androidアプリストップウォッチ

遭遇エラー

 状態:確認中  閲覧数:899  投稿日:2018-04-17  更新日:2018-04-25

一覧


▼MainActivity.kt
Unresolved reference: Handler
Unresolved reference: TextView
Unresolved reference: Button
import android.os.Handler
import android.widget.TextView
import android.widget.Button


▼activity_main.xml
Unresolved reference: timeText
  <TextView
       android:id="@+id/timeText"



This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints  The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX). These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.

「Android Studio 3.0.1」でデザインプレビューで表示されるテキストと、エミュレーター(もしくは実機デバッグ)で表示されるテキスト内容が異なる



▼MainActivity.kt
package work.w4c.android0.kotlinstopwatch20180418

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.TextView
import android.widget.Button

class MainActivity : AppCompatActivity() {
   // 1度だけ代入するものはvalを使う
   val handler = Handler()
   // 繰り返し代入するためvarを使う
   var timeValue = 0

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)
       // View要素を変数に代入
       //val timeText = findViewById(R.id.timeText) as TextView
       //val startButton = findViewById(R.id.start) as Button
       //val stopButton = findViewById(R.id.stop) as Button
       //val resetButton = findViewById(R.id.reset) as Button
       val timeText = findViewById<TextView>(R.id.timeText)
       val startButton = findViewById<Button>(R.id.start)
       val stopButton = findViewById<Button>(R.id.stop)
       val resetButton = findViewById<Button>(R.id.reset)


       // 1秒ごとに処理を実行
       val runnable = object : Runnable {
           override fun run() {
               timeValue++
               // TextViewを更新
               // ?.letを用いて、nullではない場合のみ更新
               timeToText(timeValue)?.let {
                   // timeToText(timeValue)の値がlet内ではitとして使える
                   timeText.text = it
               }
               //handler.postDelayed(this, 1000)
               handler.postDelayed(this, 1000)
           }

       }

       // start
       startButton.setOnClickListener {
           handler.post(runnable)
       }

       // stop
       stopButton.setOnClickListener {
           handler.removeCallbacks(runnable)
       }

       // reset
       resetButton.setOnClickListener {
           handler.removeCallbacks(runnable)
           timeValue = 0
           // timeToTextの引数はデフォルト値が設定されているので、引数省略できる
           timeToText()?.let {
               timeText.text = it
           }
       }

   }

   // 数値を00:00:00形式の文字列に変換する関数
   // 引数timeにはデフォルト値0を設定、返却する型はnullableなString?型
   private fun timeToText(time: Int = 0): String? {
       // if式は値を返すため、そのままreturnできる
       return if (time < 0) {
           null
       } else if (time == 0) {
           "00:00:00"
       } else {
           val h = time / 3600
           val m = time % 3600 / 60
           val s = time % 60
           "%1$02d:%2$02d:%3$02d".format(h, m, s)
       }
   }
}


▼activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/timeText"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="00:00:00"
       android:textSize="60sp"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       tools:text="00:00:00" />

   <Button
       android:id="@+id/start"
       android:layout_width="86dp"
       android:layout_height="wrap_content"
       android:layout_marginEnd="150dp"
       android:layout_marginStart="143dp"
       android:layout_marginTop="32dp"
       android:text="start"
       app:layout_constraintEnd_toStartOf="@+id/reset"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toBottomOf="@+id/timeText" />

   <Button
       android:id="@+id/stop"
       android:layout_width="86dp"
       android:layout_height="wrap_content"
       android:layout_marginEnd="32dp"
       android:layout_marginStart="32dp"
       android:layout_marginTop="32dp"
       android:text="stop"
       app:layout_constraintEnd_toStartOf="@+id/reset"
       app:layout_constraintHorizontal_bias="0.5"
       app:layout_constraintStart_toEndOf="@+id/start"
       app:layout_constraintTop_toBottomOf="@+id/timeText" />

   <Button
       android:id="@+id/reset"
       android:layout_width="86dp"
       android:layout_height="wrap_content"
       android:layout_marginEnd="136dp"
       android:layout_marginStart="32dp"
       android:layout_marginTop="32dp"
       android:text="reset"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toEndOf="@+id/start"
       app:layout_constraintTop_toBottomOf="@+id/timeText" />

</android.support.constraint.ConstraintLayout>


正式採用の「Kotlin」で挑戦! 初めてのAndroidアプリ開発 〜ストップウォッチを作ってみよう〜
KotlinをAndroid Studio 3.0 Beta 6 でやってみた!




週間人気ページランキング / 9-15 → 9-21
順位 ページタイトル抜粋 アクセス数
アクセスが、ありませんでした! 0
2024/9/22 1:02 更新