C# Xamarin Android - 在 onCreate() 之外更改按钮属性
C# Xamarin Android - Change button properties outside of onCreate()
我已经设置了一个带有一些按钮的 activity。一个按钮用于启动蓝牙连接。所以 activity 启动,在某个时候用户点击 "Start Bluetooth",然后建立蓝牙连接,完成后我想启用另一个按钮以获得更多选项。
我的问题:如何在 activity 的 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");
}
}
}
}
在 activity
级别声明您的 button
并在 OnCreate
中初始化它,然后您可以在当前 activity
的任何地方使用它。像这样
public class bluetoothConnectionActivity : AppCompatActivity
{
Button buttonConnect;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.activity_bluetoothConnection);
buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == constants.RESULT_OK)
{
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has been activated");
buttonConnect.Enabled = true;
}
}
}
我已经设置了一个带有一些按钮的 activity。一个按钮用于启动蓝牙连接。所以 activity 启动,在某个时候用户点击 "Start Bluetooth",然后建立蓝牙连接,完成后我想启用另一个按钮以获得更多选项。
我的问题:如何在 activity 的 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");
}
}
}
}
在 activity
级别声明您的 button
并在 OnCreate
中初始化它,然后您可以在当前 activity
的任何地方使用它。像这样
public class bluetoothConnectionActivity : AppCompatActivity
{
Button buttonConnect;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.activity_bluetoothConnection);
buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == constants.RESULT_OK)
{
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has been activated");
buttonConnect.Enabled = true;
}
}
}