[英]Android studio TextView value won't update
我目前正在开发一个应用程序,该应用程序从一氧化碳传感器接收蓝牙数据,然后将其显示给用户手机。 我有蓝牙方面的工作,我正在接收我需要的数据但是,当试图将变量显示到 TextView 元素时,元素不会更新它只是保持不变。
屏幕上的按钮(在下面的 xml 中)将触发另一个模块以启动蓝牙通信,然后应该显示在屏幕上。 这是按钮的 xml 和 TextView
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="350dp"
android:layout_marginBottom="160dp"
android:gravity="center"
**android:onClick="onStart"**
android:text="Start"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/textViewCO"
android:layout_width="53dp"
android:layout_height="14dp"
android:layout_marginEnd="350dp"
android:layout_marginBottom="408dp"
**android:text="Test"**
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
这是我目前为尝试完成这项工作而设置的代码。
while (btSocket.isConnected()) {
//reading the bluetooth input
info = inputStream.read(buffer);
//assigning read value from the buffer to a string
String readMessage = new String(buffer, 0, info);
//New textview object
TextView tv = (TextView) findViewById(R.id.textViewCO);
//Display variable
tv.setText(readMessage);
//Console log
System.out.println(readMessage);
try {
Thread.sleep(3200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
我已经尝试过。 我试过简单地显示文本,例如
tv.setText("This is some text");
然而,这也不会改变任何事情。 如果您需要任何其他代码或信息,请告诉我。
似乎您正在线程中运行代码,有时这会影响用户界面的性能,请尝试使用 ASYNC 任务,如果失败,请在使用 scrollBy 方法执行代码后尝试滚动父视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.