安卓开发中,revTemp.setText(ReceiveTemp);界面还是显示为0,但值已经不为0,不知道什么原因?
这个是mian页面的
<FrameLayout
android:id="@+id/id_content"
android:layout_width="355dp"
android:layout_weight="1"
android:layout_height="0dp">
这个是在collect页面的
<TextView
android:id="@+id/ReceiveTemp"
android:layout_width="90dp"
android:layout_height="55dp"
android:layout_gravity="center"
android:text="0"
android:textSize="40sp"
android:gravity="center"
android:textColor="@color/black"
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
LayoutInflater inflater3 = getLayoutInflater();
View layout3 = inflater3.inflate(R.layout.activity_collect,findViewById(R.id.ReceiveTemp));
revTemp = layout3.findViewById(R.id.ReceiveTemp);
}
private void rcvMsgHandler() {
new Thread(() -> {
while (true) {
String msg = rcvMsg();
if (msg != null) {
try {
object = new JSONObject(msg);
try {
String ReceiveTemp = object.getString("temperature");
revTemp.setText(ReceiveTemp);
Log.v("温度:", ReceiveTemp);
} catch (JSONException jsonException) {
jsonException.printStackTrace();
界面显示一直为0,但打印输出的有值
安卓更新UI要在子进程里面
直接赋一个固定的字符串试试,比如"15.0"
onCreateView这个里面代码贴一下,我觉得你似乎是在这里面setText()的,这个Fragment中的onCreateView,只有在创建绘制Frament的时候才会走一遍,下次不会走,你在这里面取"temperature"的值,应该是默认的0,然后setText(),下次这个"temperature"改变的时候,自然TextView还是不会变化
完全没有看懂你的代码,碎片中的布局为什么会在activity中去创建?不应该是activity创建碎片,然后碎片去创建自己的布局吗?