如何在运行时间AndroidActivity设置Action Button的图标?
How to set the icon of Action Button in Android Activity on run time?
我的 ActionBar 中有一个操作按钮,基本上用于指示 phone 上蓝牙服务的状态。我可以创建此操作按钮并切换其状态,甚至可以将图标从 bluetooth_on.png 更改为 bluetooth_off.png 但我不知道如何根据状态设置正确的图标开始activity 首次启动时的蓝牙服务。
我想我需要在这里做点什么:
@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 beacons SDK)。布尔变量 bluetoothStatus 作为我的标志变量来检查蓝牙是打开还是关闭。我如何使用它在 activity 启动时在操作按钮中设置正确的图标?
如果我的理解正确的话,您希望在启动时将图标设置为正确的指示器。假设 beaconManager 是在 onCreate 中初始化的(或者就在调用 onCreateOptionsMenu 之前),这应该对你有用。
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);
MenuItem item = menu.findItem(R.id.action_bluetooth);
if(item != null){
if(beaconManager.isBluetoothEnabled())
{
bluetoothStatus = true;
item.setIcon(R.drawable.bluetooth_on);
}else{
item.setIcon(R.drawable.bluetooth_off);
}
}
return true;
}
我的 ActionBar 中有一个操作按钮,基本上用于指示 phone 上蓝牙服务的状态。我可以创建此操作按钮并切换其状态,甚至可以将图标从 bluetooth_on.png 更改为 bluetooth_off.png 但我不知道如何根据状态设置正确的图标开始activity 首次启动时的蓝牙服务。
我想我需要在这里做点什么:
@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 beacons SDK)。布尔变量 bluetoothStatus 作为我的标志变量来检查蓝牙是打开还是关闭。我如何使用它在 activity 启动时在操作按钮中设置正确的图标?
如果我的理解正确的话,您希望在启动时将图标设置为正确的指示器。假设 beaconManager 是在 onCreate 中初始化的(或者就在调用 onCreateOptionsMenu 之前),这应该对你有用。
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);
MenuItem item = menu.findItem(R.id.action_bluetooth);
if(item != null){
if(beaconManager.isBluetoothEnabled())
{
bluetoothStatus = true;
item.setIcon(R.drawable.bluetooth_on);
}else{
item.setIcon(R.drawable.bluetooth_off);
}
}
return true;
}