[英]I want to print the toast only once when event.getX()==200. But it's showing the oast multiple times after getX()>300
frmLayout.setPadding(Math.round(event.getX() - 20), 0, 0, 0);
if(event.getX()>=300)
{
frmLayout.setPadding(20, 100, 0, 0);
}//giving minimum padding
}
switch(event.getAction())
{
case 0:
Toast.makeText(MainActivity.this, "send Function "+event.getAction(), Toast.LENGTH_SHORT).show();
// showing toast multiple yimes here
break;
}
if(event.getX()<=20)//give layout padding always x=20,y=100
{
frmLayout.setPadding(20, 100, 0, 0);
}
[編輯]
我相信案例0將檢查何時開始按下手勢。 因此,只要觸摸屏幕,就會創建Toast消息。
因此,解決方法是使用帶布爾標志的開關內部條件。
private boolean showToast = true; //Global var
switch (event.getAction()){
case 0:
if(showToast && event.getX() >= 200){
Toast.makeText(MainActivity.this, "send Function "+event.getAction(), Toast.LENGTH_SHORT).show();
showToast = false; //This will stop the condition being met the second time
break;
}
}
我沒有測試過,但是我猜這可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.