gpt4 book ai didi

java - 操作后屏幕拒绝刷新

转载 作者:行者123 更新时间:2023-12-01 14:50:25 24 4
gpt4 key购买 nike

我是java新手,目前正在开发一个项目。我正在尝试熟悉蓝牙。到目前为止,我已经能够从应用程序内打开和关闭蓝牙。我遇到的问题是执行操作后屏幕不会刷新。例如,当我打开蓝牙时,它会打开,但 TextView 中的文本不会更新,直到我翻转手机。当我断开连接时也是如此。这是我的代码

package com.example.bluetooth;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {


BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Button button;
TextView update;
CharSequence enabled = "Bluetooth Enabled";
CharSequence disabled = "Bluetooth disabled";
CharSequence connect = "Connect";
CharSequence disconnect = "Disconnect";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setUp();
action();
}

private void action() {
if (button.getText() == connect) {
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
btAdapter.enable();

}
});
}

if (button.getText() == disconnect) {
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
btAdapter.disable();

}
});
}

}

private void setUp() {
button = (Button) findViewById(R.id.button1);
update = (TextView) findViewById(R.id.textView1);



button = (Button) findViewById(R.id.button1);
if (btAdapter.isEnabled()) {

update.setText("Bluetooth is enabled");
button.setText(disconnect);

}

else {

update.setText("Bluetooth is disabled");
button.setText(connect);


}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

我认为有两件事是错误的:

  1. 您仅调用负责更新 TextView 的方法 (setIp()) 一次,因为每次创建 Activity 时仅调用一次 onCreate()。自从转adapter on/off, is done outside the UI Thread ,您必须创建一个 BroadcastReceiver 并监听 BluetoothAdapter.ACTION_STATE_CHANGED 然后进行相应更新。
  2. 您正在将 CharSequences 与 == 进行比较。您应该使用字符串,然后使用 .equals() 因为这将确保深度比较。您唯一需要更改类型声明的是在 getText() 之后添加 .toString()。例如 button.getText().toString()

编辑:一个简单(但困惑)的解决方案可以是动态地创建接收器并注册它。现在,请记住,您只需调用 setUp() 一次,就像以前一样。像下面的代码可能会起作用。

public class MainActivity extends Activity {


BluetoothAdapter btAdapter;
Button button;
TextView update;
String enabled = "Bluetooth Enabled";
String disabled = "Bluetooth disabled";
String connect = "Connect";
String disconnect = "Disconnect";
BroadcastReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btAdapter = BluetoothAdapter.getDefaultAdapter();
setUp();
action();
}

@Override
public void onPause()
{
try{
unregisterReceiver (receiver);
}
catch (NullPointerException e)
{
e.printStackTrace();
}
catch (IllegalStateException ie)
{
ie.printStackTrace();
}
super.onPause();
}

private void action() {
button.setOnClickListener(new View.OnClickListener (){

@Override
public void onClick(View v) {
if (btAdapter.isEnabled())
btAdapter.disable();
else
btAdapter.enable();

}

});

}

private void setButtonText ()
{
if (btAdapter.isEnabled()) {
update.setText("Bluetooth is enabled");
button.setText(disconnect);
}

else {

update.setText("Bluetooth is disabled");
button.setText(connect);
}
}
private void setUp() {
button = (Button) findViewById(R.id.button1);
update = (TextView) findViewById(R.id.textView1);
setButtonText();

receiver = new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
setButtonText();

}

};

IntentFilter filter = new IntentFilter (BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver (receiver, filter);

}
}

关于java - 操作后屏幕拒绝刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14927579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com