0

我可以获得 OptionsMenu 点击次数吗?

我想获得在 onReceive 中使用的价值,就像这样

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        SubMenu fileMenu = menu.addSubMenu(0, 7, Menu.NONE, "歌曲");    
        fileMenu.add(0, 1, Menu.NONE, "A");  
        fileMenu.add(0, 2, Menu.NONE, "B");  
        fileMenu.add(0, 3, Menu.NONE, "C");
        fileMenu.add(0, 4, Menu.NONE, "D");
        return true;
    }
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:

              Toast.makeText(this, "A", Toast.LENGTH_SHORT).show();
              break;
        case 2:

              Toast.makeText(this, "B", Toast.LENGTH_SHORT).show();
              break;
        case 3:

              Toast.makeText(this, "C", Toast.LENGTH_SHORT).show();
              break;
        case 4:
              Toast.makeText(this, "D", Toast.LENGTH_SHORT).show();
              break;
        default:
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
IntentFilter intent = new IntentFilter();  
intent.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(searchDevices, intent);
private BroadcastReceiver searchDevices = new BroadcastReceiver() {  
      public void onReceive(Context context, Intent intent) { 
        if(OptionsMenu click times == 2)
    {

    }
}

因为我只有这部分的过程,

请帮助我了解如何解决提供建议谢谢

4

2 回答 2

0

Bali gave a good solution for what you asked. But there is a much simpler way to get this done without using a receiver.

Make a new class that has a static container for your click number.

ie:

public class ParamValues{

private static int clickNums= 0;

    /**
     * @return the clickNumber
     */
    public static int getClickNums() {
        return clickNums;
    }

    /**
     * Sets the clickNumber
     */
    public static int setClickNum(int clickNum) {
        clickNums = clickNum;
    }
}

Then create an onClickHandler for your OptionsMenu, and inside the click handler add in somecode like.

int count = ParamValues.getClickNum();
count++;
ParamValues.setClickNum(count);

Now you could get this click count anywhere in your code. If you want to make sure it's synchronized you could just add a function to process what you want in the same onClick.

ie:

if(ParamValues.getClickNum() == 2)
    {
// Do whatever
    }

Then in you can reset the count value in here or wherever you would want to reset it by using the convenient setter in the ParamValues class.

Using the receiver you can't guarantee exactly when the code will execute.

于 2012-09-27T18:45:03.013 回答
0

根据您提供的详细信息:

您可以在 Activity 中创建一个数据成员,它将存储按下菜单键的次数,并在每次按下时增加它:

private int menuPressedCount = 0;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_MENU)
        menuPressedCount++;
}

你可以把它传递给你的接收者,把它放在 Intent 中,接收者会得到:

Intent intent = ...;
intent.putExtra("menu_pressed_count", menuPressedCount);

在 onReceive() 中:

int pressedCount = intent.getIntExtra("menu_pressed_count", 0);

告诉我这不是你要找的!

于 2012-09-27T18:12:45.173 回答