setOnClickListener and setOnLongClickListener call on single button issue

左心房为你撑大大i 提交于 2019-12-18 12:24:46

问题


I need your help if any one can be, it will be great thing for my solution. I don't know is it possible or not, but I want to try to fix this out any how.. Actually I want to implement two method on single button click event, its simple click and long click, here my code ::

homebutton = (ImageButton) findViewById(R.id.home_icon);
homebutton.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        Intent intent = new Intent(context, MainActivity.class);
        startActivity(intent);
    }
});
homebutton.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View arg0) {
        Toast.makeText(getApplicationContext(), "Long Clicked " , Toast.LENGTH_SHORT).show();
        return false;
    }
});

So, here i am getting something wrong, even single click is working perfectly, and long click is also working, but problem is that after long click event its also start MainActivity as defined in above code of onClick method..

That should not be done, return false is also there, still not working as i want.. So, anybody please help me to get it resolve..

Thanks in Advance..


回答1:


I believe you need to return TRUE in your onLongClick method - telling the framework that the touch event is consumed and no further event handling is required.

homebutton.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View arg0) {
        Toast.makeText(getApplicationContext(), "Long Clicked " ,
              Toast.LENGTH_SHORT).show();

        return true;    // <- set to true
    }
});


来源:https://stackoverflow.com/questions/13470404/setonclicklistener-and-setonlongclicklistener-call-on-single-button-issue

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