[英]Android studio: How do I show an updating variable at all times using a Textview?
[英]How do I show data in Textview for a 2 second (android Studio)?
I want to show data in Textview for 2 second time.So I recieved data from arduino that not have a delay between I use serialwrite.I use InputStream and DatainputStream for recieved data.It's still have a problem when data show in textview.It will突然獲得下一個數據的機會。有沒有什么方法可以保持2秒並抓住下一個數據? 我在 android Studio 中編寫代碼。 .
private void receive(byte[] data) {
InputStream is = null;
DataInputStream dis = null;
try {
is = new ByteArrayInputStream(data);
dis = new DataInputStream(is);
while (dis.available() > 0) {
byte[] b = new byte[data.length];
dis.read(b, 0, 8);
count++;
String part = HexDump.dumpHexString(b).substring(HexDump.dumpHexString(b).indexOf("E7 0"), HexDump.dumpHexString(b).indexOf("0A"));
if (part.contains("E7 01")) {
//receive_text2.setText((part));
show_text(receive_text2,part);
}
if (part.contains("E7 02")) {
receive_text3.setText(String.valueOf(1000*unsiged(b[5])+100*unsiged(b[6])));
}
if (part.contains("E7 03")) {
receive_text4.setText((part));
}
if (part.contains("E7 05")) {
receive_text5.setText((part));
}
receive_text6.setText(String.valueOf(count));
}
}catch (Exception e){
receive_text2.append("");
receive_text3.append("");
receive_text4.append("");
receive_text5.append("");
receive_text6.append("");
}
}
每次嘗試設置文本值時,您都可以嘗試使用 Handler 延遲 2 秒(2000 毫秒)。
TextView textView = ...;
textView.postDelayed(new Runnable() {
@Override
public void run() {
textView.setText("some text");
}
}, 2000); // delay of 2 seconds before setting a text to textView
將要顯示的所有字符串添加到 Arraylist
ArrayList<String> strings = new ArrayList<>();
然后做:
Runnable job = new Runnable() {
@Override
public void run() {
if (!strings.isEmpty()) {
textView.setText(strings.get(0));
strings.remove(0);
textView.postDelayed(this, 2000);
} else {
textView.removeCallbacks(this);
}
}
};
textView.postDelayed(job, 2000); // delay of 2 seconds before setting a text to textView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.