SHARE
TWEET

Untitled

a guest Sep 15th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.lab1_tiisu_android
  2.  
  3. import androidx.appcompat.app.AppCompatActivity
  4. import android.os.Bundle
  5. import androidx.core.app.ComponentActivity
  6. import androidx.core.app.ComponentActivity.ExtraData
  7. import androidx.core.content.ContextCompat.getSystemService
  8. import android.icu.lang.UCharacter.GraphemeClusterBreak.T
  9. import android.widget.Button
  10. import android.widget.TextView
  11.  
  12.  
  13. // "implements TiisuThread.listener"
  14. class MainActivity : AppCompatActivity(), TiisuThread.Listener {
  15.  
  16.     lateinit var tiisuTextView: TextView
  17.     lateinit var tiisuThread: TiisuThread
  18.  
  19.     // "implements TiisuThread.listener"
  20.     override fun onUpdate(teksti: String?) {
  21.         runOnUiThread(Thread(Runnable {
  22.             // päivitetään textViewviä - huom. tarvitaan runOnUiThread.
  23.             tiisuTextView.append(teksti +"\n")
  24.         }))
  25.     }
  26.  
  27.     override fun onCreate(savedInstanceState: Bundle?) {
  28.         super.onCreate(savedInstanceState)
  29.         setContentView(R.layout.activity_main)
  30.  
  31.         tiisuTextView = findViewById(R.id.textView)
  32.         tiisuThread = TiisuThread(this)
  33.         findViewById<Button>(R.id.startBtn).setOnClickListener {
  34.             if(!tiisuThread.isAlive) {
  35.                 tiisuThread = TiisuThread(this)
  36.                 tiisuThread.start()
  37.             } else {
  38.                 // threadi on jo päällä, ja start nappia painettiin.
  39.             }
  40.         }
  41.  
  42.         findViewById<Button>(R.id.stopBtn).setOnClickListener {
  43.             if(tiisuThread.isAlive) {
  44.                 // laitetaan threadi pois päältä. Se sammuu heti kun se poistuu run() metodista.
  45.                 tiisuThread.running = false
  46.             } else {
  47.                 // threadi on jo koullut ja start nappia painettiin
  48.             }
  49.         }
  50.  
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top