如何延迟打印文本视图中的数组元素
How to print array elements in textview with a delay
我试图在同一个文本视图中一个一个地打印字符串数组的所有元素,每个元素之间有 1 秒的延迟,但唯一打印的是最后一个元素。
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv2;
private String []numbers={"1","2","3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2=findViewById(R.id.tv2);
}
public void initiate(View view){
for (int i = 0; i < numbers.length; i++){
tv2.setText(numbers[i]);
tv2.postDelayed(new Runnable() {
@Override
public void run() {
tv2.setText("");
}
},1000);
}
}
}
试试这个
更新
public void initiate(View view){
int i = 0;
new CountDownTimer((number.lenght*10000),1000){
@SuppressLint("SetTextI18n")
@Override
public void onTick(long millisUntilFinished) {
if(i<numbers.length)
tv2.setText(numbers[i++]);
}
@Override
public void onFinish() {
}
}.start();
}
请您像下面这样试一下好吗?
class DispatchGroupManager {
var count: Int = 0
var runnable: Runnable? = null
constructor() {
count = 0
}
@Synchronized fun enter() {
count ++
}
@Synchronized fun leave() {
count --
notifyGroup()
}
fun notify(r: Runnable) {
runnable = r
notifyGroup()
}
fun notifyGroup() {
if(count <= 0 && runnable != null) {
runnable!!.run()
}
}
}
val requestGroup = DispatchGroupManager()
for(t in this.numbers) {
requestGroup.enter()
// tv2.setText(numbers[i]);
Handler(Looper.getMainLooper()).postDelayed(object : Runnable {
override fun run() {
//tv2.setText("");
requestGroup.leave()
}
},1000)
}
requestGroup.notify(Runnable {
})
我试图在同一个文本视图中一个一个地打印字符串数组的所有元素,每个元素之间有 1 秒的延迟,但唯一打印的是最后一个元素。
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv2;
private String []numbers={"1","2","3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2=findViewById(R.id.tv2);
}
public void initiate(View view){
for (int i = 0; i < numbers.length; i++){
tv2.setText(numbers[i]);
tv2.postDelayed(new Runnable() {
@Override
public void run() {
tv2.setText("");
}
},1000);
}
}
}
试试这个
更新
public void initiate(View view){
int i = 0;
new CountDownTimer((number.lenght*10000),1000){
@SuppressLint("SetTextI18n")
@Override
public void onTick(long millisUntilFinished) {
if(i<numbers.length)
tv2.setText(numbers[i++]);
}
@Override
public void onFinish() {
}
}.start();
}
请您像下面这样试一下好吗?
class DispatchGroupManager {
var count: Int = 0
var runnable: Runnable? = null
constructor() {
count = 0
}
@Synchronized fun enter() {
count ++
}
@Synchronized fun leave() {
count --
notifyGroup()
}
fun notify(r: Runnable) {
runnable = r
notifyGroup()
}
fun notifyGroup() {
if(count <= 0 && runnable != null) {
runnable!!.run()
}
}
}
val requestGroup = DispatchGroupManager()
for(t in this.numbers) {
requestGroup.enter()
// tv2.setText(numbers[i]);
Handler(Looper.getMainLooper()).postDelayed(object : Runnable {
override fun run() {
//tv2.setText("");
requestGroup.leave()
}
},1000)
}
requestGroup.notify(Runnable {
})