Android onOptionsItemSelected避免双击

时间:2015-06-12 10:24:49

标签: android android-menu

如何避免双击我的示例,任何解决方案?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.testing) {
                Dialog();
    return super.onOptionsItemSelected(item);
}

3 个答案:

答案 0 :(得分:2)

实现这一目标有很多方法。我只是举例说明。

只需在Activity类中创建一个布尔变量。

Boolean isClicked = false;

然后

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.testing) {
    if (!isClicked){
//Change here as your flag is true
isClicked = true;
                    Dialog();
    }
        return super.onOptionsItemSelected(item);
    }

然后此对话框只显示一次。如果需要任何更改,请询问。

答案 1 :(得分:0)

Sadly, but nothing from below options worked for me..

  1. Disabling view clicability - appeared to be too slow for "MAD CLICKER"
  2. setting some boolean (like isClicked to true) on click and checking it in onClickListener - looks fast, but not enough

Finally got a method which prevents "mad double click behaviour" on Menu items, which is even worse than double click on simple button, imo.

// define Fragment / Activity variable

volatile private byte saveClickCounter = 0;

// implement

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {            
        case R.id.common_action_save:

            if (saveClickCounter++ == 0) {
                saveButtonClick();
            } else {
                Log.i(TAG, "OMG! U R fast!");
            }

            return true;
    }

    return super.onOptionsItemSelected(item);
}

Don't forget to make that variable equal to zero again (or decrease that), after successfull method call.

saveClickCounter--;

答案 2 :(得分:0)

另一种简单的方法与@AlexV相似,请使用增量运算符

创建全局变量

private var saveClickCounter: Int = 0

在onOptionsItemSelected中

val id = item.itemId
    if (id == R.id.save)
    {
        if (saveClickCounter == 0) {
            Log.d(TAG, "clickedd")
            saveClickCounter++
        } else {
            Log.d(TAG, "OMG! U R fast!");
        }
    }