如何更改回调中按钮的文本?(How to change the text of a button in a callback ? (Android))

编程入门 行业动态 更新时间:2024-10-19 23:47:56
如何更改回调中按钮的文本?(How to change the text of a button in a callback ? (Android))

这是我的应用程序:它执行蓝牙扫描,扫描功能在找到设备时进行回调。 我想在找到特定设备(它用设备名称识别)后,将特定按钮的文本从“搜索”更改为“连接”。

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

有没有办法做到这一点? 我认为这纯粹是一个范围问题,我对这类事情几乎没有经验。

代码:

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 
  
 

Here's my app : It does a bluetooth scan, and the scanning function has callback when it finds a device. I would like to change the text of a particular button from "Searching" to "Connect" once it finds a particular device (which it recognizes with its device name).

However the button is not reachable in the scope of the callback.

Is there any way to do this ? I think this is purely a scope problem, and I have little experience with this kind of things.

Code :

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); } }); } } };

Compiler error log :

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 
  
 

最满意答案

当在方法体内定义匿名内部类时,可以从内部类中访问在该方法范围内声明为final的所有变量。 对于标量值,一旦分配了标量值,最终变量的值就不会改变。 对于对象值,引用不能更改。 这允许Java编译器在运行时“捕获”变量的值,并将副本存储为内部类中的字段。 一旦外部方法终止并且其堆栈框架已被移除,原始变量就会消失,但内部类的私有副本仍然存在于类的自己的内存中。

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); } }); } } };

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); } }); } } };

更多推荐

本文发布于:2023-07-24 00:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   按钮   如何更改   文本   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!