LongClick event also triggers Click event

余生颓废 提交于 2019-12-04 16:39:43

问题


I use onLongClick and onClick events of a button to get user inputs. Whenever; the user long click and triggers onLongClick event, the onClick event is also triggered. I couldn't find my problem. The code of two methods are shown in below:

@Override
    public void onClick(View v) {
        switch(((Button) v).getId())
        {
        case R.id.enter:
            EntertheNumber();
            break;
        case R.id.clear:
            CleartheNumber();
            break;
        case R.id.number_zero:
        case R.id.number_one:
        case R.id.number_two:
        case R.id.number_three:
        case R.id.number_four:
        case R.id.number_five:
        case R.id.number_six:
        case R.id.number_seven:
        case R.id.number_eight:
        case R.id.number_nine:
            AddtotheNumber(mEditor, (Button) v);
            break;
        }
@Override
    public boolean onLongClick(View view) {
        if(SMBGuesstheNumber.bDisplayFlagList)
        {
            theActiveButton = (Button) view;
            showDialog(R.id.display_flaglist);
        }
        return false;
    }

Actually, my project is Open Source. So, you can find all the code at http://code.google.com/p/guessthenumber/

Thank you.


回答1:


I'm not sure what order these events occur but the onLongClick handler returns a bool to indicate whether the event was handled. You should return true if you handled it so that other click events will not be called. I don't know if this will prevent prevent the onClick() from firing though.

You may also turn these events off and on using setClickable(boolean) and setLongClickable(boolean)

You can find this information and more about UI events here.




回答2:


here is a brief summary regarding touch event: http://rxwen.blogspot.com/2010/10/android-touch-event-summary.html




回答3:


Solution On LongClick SingleClick will not work :

rippleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //Your code
            }
        });
rippleView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });


来源:https://stackoverflow.com/questions/742171/longclick-event-also-triggers-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!