【发布时间】:2018-12-14 15:45:32
【问题描述】:
我用一些按钮设置了一个活动。一个按钮用于启动蓝牙连接。所以活动开始了,在某些时候用户点击“开始蓝牙”,然后蓝牙连接建立,完成后我想启用另一个按钮以获得更多选项。
我的问题:如何在活动的 onCreate() 函数之外将按钮从 Enabled = False 更改为 Enabled = True? 我的代码:在 onActivityResult() 方法中,我想启用按钮 buttonConnect。我的解决方案似乎不起作用。
我只能找到 onClickListeners,但这不是我需要的...
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Bluetooth;
using Android.Util;
using Android.Content;
using System.Reflection;
namespace firstTry3
{
[Activity(Label = "@string/app_name")]
public class bluetoothConnectionActivity : AppCompatActivity
{
BluetoothAdapter mBluetoothAdapter;
const int REQUEST_ENABLE_BT = 1; //necessary to start bluetooth, must be greater than 0
string tag = "blueApp";
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_bluetoothConnection);
Button buttonBluetoothOn = FindViewById<Button>(Resource.Id.bluetoothOn);
Button buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
Button buttonDissconnect = FindViewById<Button>(Resource.Id.disconnectButton);
mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
buttonBluetoothOn.Click += (sender, e) =>
{
if (!mBluetoothAdapter.IsEnabled)
{
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Device does not have bluetooth capabilities");
}
enableBluetooth();
};
}
public void enableBluetooth() {
if(mBluetoothAdapter == null)
{
Log.Warn(tag, MethodBase.GetCurrentMethod().Name + ": Device does not have bluetooth capabilities");
}
if (!mBluetoothAdapter.IsEnabled)
{
Intent enableBTIntent = new Intent(BluetoothAdapter.ActionRequestEnable);
StartActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == constants.RESULT_OK)
{
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has been activated");
Button buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
buttonConnect.Enabled = true;
}
if (resultCode == constants.RESULT_CANCELLED){
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has NOT been activated");
}
else
{
Log.Error(tag, MethodBase.GetCurrentMethod().Name + ": invalid resultCode");
}
}
}
}
【问题讨论】:
标签: c# xamarin button xamarin.android oncreate