-
-
Save nicemak/999822bda294780efeedde0637658d85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.livedatatest | |
import android.content.Intent | |
import android.os.Bundle | |
import android.util.Log | |
import android.widget.Button | |
import android.widget.EditText | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.widget.addTextChangedListener | |
import androidx.lifecycle.lifecycleScope | |
import kotlinx.coroutines.coroutineScope | |
import kotlinx.coroutines.* | |
import kotlinx.coroutines.runBlocking | |
import java.util.* | |
//import implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1' | |
//import implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' | |
class MainActivity : AppCompatActivity() { | |
lateinit var editText:EditText | |
lateinit var editText2:EditText | |
var number=0 | |
private lateinit var timer: Job | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
editText=findViewById<EditText>(R.id.editText) | |
editText2=findViewById<EditText>(R.id.editText2) | |
var star="*" | |
editText.addTextChangedListener { | |
it!!.length | |
// runBlocking { | |
// delay(1000L) | |
// editText2.setText("*") | |
// } | |
lifecycleScope.launch(Dispatchers.Main) { | |
var tempString="" | |
for(times in 1..it.length){ | |
tempString += "*" | |
} | |
delay(500L) | |
editText2.setText(tempString) | |
} | |
// Timer().schedule("this",1000L) | |
} | |
timer= startCoroutineTimer(delayMillis = 0, repeatMillis = 100) { | |
number++ | |
Log.i("Timer ran","running $number") | |
if (number==100) | |
timer.cancel() | |
} | |
// timer.cancel() | |
} | |
private inline fun startCoroutineTimer(delayMillis: Long = 0, repeatMillis: Long = 0, crossinline action: () -> Unit) = GlobalScope.launch { | |
delay(delayMillis) | |
if (repeatMillis > 0) { | |
while (true) { | |
action() | |
delay(repeatMillis) | |
} | |
} else { | |
action() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment