如何在回调中更改按钮的文本? (Android)

How to change the text of a button in a callback ? (Android)

这是我的应用程序: 它进行蓝牙扫描,扫描函数在找到设备时有回调。 一旦找到特定设备(它用设备名称识别),我想将特定按钮的文本从 "Searching" 更改为 "Connect"。

但是在回调范围内无法访问按钮。

有什么办法吗?我认为这纯粹是一个范围问题,我对这种事情没有什么经验。

代码:

 Context context;
    context = this;

    update_str = "";

    Button ConnectButton = (Button) findViewById(R.id.Connect);
    ConnectButton.setText("Waiting for device to be found");

    Button ScanButton = (Button) findViewById(R.id.Scan);

    ScanButton.setOnClickListener(new View.OnClickListener() {
                                      public void onClick(View v) {
                                          btAdapter.startLeScan(leScanCallback);
                                      }
                                  });

    BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
        public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {

            update_str = update_str.concat(" " + device.getName());
            ((TextView)findViewById (R.id.text)).setText (update_str);
            nom_device = device.getName();
            if (nom_device=="bill_gates"){

                context.ConnectButton.setText(nom_device);

                context.ConnectButton.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, context.btleGattCallback);
                    }

            });
        }

        }
    };

编译器错误日志:

C:\Code\Grizz\app\src\main\java\com\grizz\grizzmvp\MainActivity.java
Error:(84, 28) error: cannot find symbol variable ConnectButton
Error:(89, 117) error: cannot find symbol variable btleGattCallback
Error:(86, 28) error: cannot find symbol variable ConnectButton
Note: C:\Code\Grizz\app\src\main\java\com\grizz\grizzmvp\MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1.381 secs
Information:4 errors
Information:0 warnings
Information:See complete output in console

尝试:

  Context context ; //Global
  context=this;  //In oncreate of activity,

 // construction of button 
 Button ConnectButton = (Button) findViewById(R.id.Connect);
 ((Button)findViewById(R.id.ConnectButton)).setText("Searching.."); 

 public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {

            nom_device = device.getName();
            if (nom_device=="bill_gates"){ 
                ((Button)findViewById(R.id.ConnectButton)).setText("Connect");

              // This part fails because the callback doesn't recognize ConnectButton. It's out of his scope. 
                context.ConnectButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, btleGattCallback);
                    } 

            }); 

When an anonymous inner class is defined within the body of a method, all variables declared final in the scope of that method are accessible from within the inner class. For scalar values, once it has been assigned, the value of the final variable cannot change. For object values, the reference cannot change. This allows the Java compiler to "capture" the value of the variable at run-time and store a copy as a field in the inner class. Once the outer method has terminated and its stack frame has been removed, the original variable is gone but the inner class's private copy persists in the class's own memory.

final Button ConnectButton = (Button) findViewById(R.id.Connect);
ConnectButton.setText("Waiting for device to be found");

final Button ScanButton = (Button) findViewById(R.id.Scan);
ScanButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        btAdapter.startLeScan(leScanCallback);
    }
});

BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
    public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
        update_str = update_str.concat(" " + device.getName());
        ((TextView)findViewById (R.id.text)).setText (update_str);
        nom_device = device.getName();
        if (nom_device.equals("bill_gates")){

            ConnectButton.setText(nom_device);
            ConnectButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, context.btleGattCallback);
                }
            });
        }
    }
};