[英]Android - Can I use setOnLongClickListener and setOnClickListener for same button?
Can I really use these setOnLongClickListener and setOnClickListener for same button? 我真的可以将这些setOnLongClickListener和setOnClickListener用于同一个按钮吗? Because if I long click the button both longclick and normal click will be executed and I dont know why.
因为如果我长按单击按钮,将执行longclick和正常点击,我不知道为什么。 Can I really do this?
我真的可以这样做吗? Please help me:)
请帮我:)
readDbButton.setOnLongClickListener(
new View.OnLongClickListener() {
public boolean onLongClick(View view) {
//do something
return false;
}
}
);
readDbButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
//Do something else
}
});
return TRUE in your onLongClick
method so that the event will be consumed. 在
onLongClick
方法中返回TRUE,以便消耗该事件。
readDbButton.setOnLongClickListener(
new View.OnLongClickListener() {
public boolean onLongClick(View view) {
//do something
return true;
}
}
);
I got the solution of your Question.Return true instead of false in LongPressed.Just see below:- 我得到了你的问题的解决方案。在LongPressed中返回true而不是false。见下文: -
readDbButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();
return true;
}
});
That is, return true to indicate that you have handled the event and it should stop here; 也就是说,返回true表示你已经处理了这个事件,它应该在这里停止; return false if you have not handled it and/or the event should continue to any other on-click listeners
如果您尚未处理它并且/或该事件应继续到任何其他点击监听器,则返回false
Try this proper way to Implement this 尝试这种正确的方法来实现它
public class MainActivity extends Activity {
private Button button;
private GestureDetector gestureDetector;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this, new MyGestureDetector());
button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent ev) {
return gestureDetector.onTouchEvent(ev);
}
});
}
private class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Toast.makeText(MainActivity.this, "Single Tap", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(MainActivity.this, "Long Tap", Toast.LENGTH_SHORT).show();
}
}
}
readDbButton.setOnLongClickListener(
new View.OnLongClickListener() {
public boolean onLongClick(View view) {
//do something
Toast.makeText(getBaseContext(), "Long click", Toast.LENGTH_SHORT).show();
return true;
}
}
);
readDbButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Toast.makeText(getBaseContext(), "onclick", Toast.LENGTH_SHORT).show();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.