我有一个更新的TextView,但在最小化并重新打开应用程序之前,我无法看到刷新。这是执行该操作的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bleAdapter = ((BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter();
Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
device.connectGatt(this, true, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
TextView state;
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
state = (TextView)findViewById(R.id.state);
state.setText("Connected = True");
state.setTextColor(Color.GREEN);
break;
case BluetoothProfile.STATE_DISCONNECTED:
state = (TextView)findViewById(R.id.state);
state.setText("Connected = False");
state.setTextColor(Color.RED);
break;
}
}
});
}
}
我需要刷新TextView吗?我做错了什么?
答案 0 :(得分:4)
您应该更新主(ui)主题中的 TextView ,就像这样
runOnUiThread(new Runnable() {
public void run() {
state.setText("Connected = False");
state.setTextColor(Color.RED);
}
});