0
我在我的ActionBar中有一個Action Button,它將基本上用於指示手機上藍牙服務的狀態。我可以創建此動作按鈕並切換其狀態,甚至將圖標從bluetooth_on.png更改爲bluetooth_off.png,但我不知道如何根據藍牙服務的狀態設置正確的圖標在活動首次啓動時。如何在運行時設置Android Activity中的Action Button圖標?
我想我需要在這裏做一些事情:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
這是我與迄今使用的代碼:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id == R.id.action_bluetooth)
{
if(bluetoothStatus == false)
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
if(beaconManager.isBluetoothEnabled())
{
bluetoothStatus = true;
item.setIcon(R.drawable.bluetooth_on);
}
}
else
{
bluetooth_mgr.disable();
bluetoothStatus = false;
item.setIcon(R.drawable.bluetooth_off);
}
}
return super.onOptionsItemSelected(item);
}
忽略beaconManager(我使用的是Estimotes信標SDK)。布爾變量bluetoothStatus作爲我的標誌變量來檢查藍牙是否打開或關閉。如何在活動啓動時使用此功能在操作按鈕中設置正確的圖標?
是的,這正是我想要的!非常感謝 :) – 2015-04-04 00:28:05