如何每 5 秒在值之间切换 TextView
How to switch TextView between values every 5 seconds
我不知道使用 Threads 是否是我想要实现的实践,所以请随意修改您希望的代码。
我正在尝试在我的应用程序中实现一项功能,其中 TextView
切换到 1 个值,此后每 5 秒恢复到初始值。我在一个线程上实现了以下使用:
layoutDate.setText(firstNumber);
Thread numberSwitch = new Thread() {
@Override
public void run() {
while(!isInterrupted()) {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable () {
@Override
public void run() {
layoutDate.setText(secondNumber);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
numberSwitch.start();
我已经尝试在 runUiOnThread
之后添加 layoutDate.setText(firstNumber)
,也在嵌套的 run
之后添加,但它仍然无法正常工作。
我想实现如下目标:
验证字段中的当前数字并更改为另一个,您可以通过获取值 layoutDate.getText().toString()
或使用新变量来存储该值来实现。
layoutDate.setText(firstNumber);
Thread numberSwitch = new Thread() {
@Override
public void run() {
while(!isInterrupted()) {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable () {
@Override
public void run() {
if(currentNumber == firstNumber){
currentNumber = secondNumber;
}else{
currentNumber = firstNumber;
}
layoutDate.setText(currentNumber);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
numberSwitch.start();
或者
if(layoutDate.getText().toString().equals(firstNumber+"")){
layoutDate.set(secondNumber);
}else{
layoutDate.set(firstNumber);
}
您可以将处理程序与可运行对象一起使用。
private TextView textView;
private Handler handler;
在你的onCreate()
textView = findViewById(R.id.tv_bt_msg);
textView.setText("secondNumber");
handler = new Handler();
//Change the textview after 5 seconds
handler.postDelayed(runnable, 5000);
可运行以每 5 秒更改一次文本
private Runnable runnable = new Runnable() {
@Override
public void run() {
String currentString = textView.getText().toString();
if(currentString.equals("secondNumber")){
currentString = "firstNumber";
}else{
currentString = "secondNumber";
}
final String tempString = currentString;
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(tempString);
}
});
//Change the textview again after 5 seconds
handler.postDelayed(runnable, 5000);
}
};
使用它来删除回调
handler.removeCallbacks(runnable);
我不知道使用 Threads 是否是我想要实现的实践,所以请随意修改您希望的代码。
我正在尝试在我的应用程序中实现一项功能,其中 TextView
切换到 1 个值,此后每 5 秒恢复到初始值。我在一个线程上实现了以下使用:
layoutDate.setText(firstNumber);
Thread numberSwitch = new Thread() {
@Override
public void run() {
while(!isInterrupted()) {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable () {
@Override
public void run() {
layoutDate.setText(secondNumber);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
numberSwitch.start();
我已经尝试在 runUiOnThread
之后添加 layoutDate.setText(firstNumber)
,也在嵌套的 run
之后添加,但它仍然无法正常工作。
我想实现如下目标:
验证字段中的当前数字并更改为另一个,您可以通过获取值 layoutDate.getText().toString()
或使用新变量来存储该值来实现。
layoutDate.setText(firstNumber);
Thread numberSwitch = new Thread() {
@Override
public void run() {
while(!isInterrupted()) {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable () {
@Override
public void run() {
if(currentNumber == firstNumber){
currentNumber = secondNumber;
}else{
currentNumber = firstNumber;
}
layoutDate.setText(currentNumber);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
numberSwitch.start();
或者
if(layoutDate.getText().toString().equals(firstNumber+"")){
layoutDate.set(secondNumber);
}else{
layoutDate.set(firstNumber);
}
您可以将处理程序与可运行对象一起使用。
private TextView textView;
private Handler handler;
在你的onCreate()
textView = findViewById(R.id.tv_bt_msg);
textView.setText("secondNumber");
handler = new Handler();
//Change the textview after 5 seconds
handler.postDelayed(runnable, 5000);
可运行以每 5 秒更改一次文本
private Runnable runnable = new Runnable() {
@Override
public void run() {
String currentString = textView.getText().toString();
if(currentString.equals("secondNumber")){
currentString = "firstNumber";
}else{
currentString = "secondNumber";
}
final String tempString = currentString;
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(tempString);
}
});
//Change the textview again after 5 seconds
handler.postDelayed(runnable, 5000);
}
};
使用它来删除回调
handler.removeCallbacks(runnable);