【问题标题】:TouchEvent executing both if and else partTouchEvent 同时执行 if 和 else 部分
【发布时间】:2012-05-30 06:38:01
【问题描述】:

在下面的代码中,我面临的问题是在 onTouchListener 方法中,if 和 else 部分每次都被执行。有人知道为什么会这样吗?并且我的项目的目标是在 web 视图中的任何位置发生触摸事件,停靠布局必须出现在另一个触摸上,它必须消失。有人可以帮忙吗?

提前谢谢..

     public class MathiasdockActivity extends Activity {
    /** Called when the activity is first created. */
    RelativeLayout upperdock,parent;
    LinearLayout tocbottom,tocparent;
    boolean flag=true;
    WebView wv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        parent=(RelativeLayout) findViewById(R.id.rl1);
        upperdock=(RelativeLayout) findViewById(R.id.upperdock);
        tocparent=(LinearLayout) findViewById(R.id.tocparent);
        tocbottom=(LinearLayout) findViewById(R.id.linearLayout3);
        upperdock.setVisibility(RelativeLayout.INVISIBLE);
        tocparent.setVisibility(LinearLayout.INVISIBLE);
        tocbottom.setVisibility(LinearLayout.INVISIBLE);
        wv = (WebView) findViewById(R.id.webView1);  
        wv.loadUrl("file:///android_asset/hotspot/07-03.html");
        wv.getSettings().setJavaScriptEnabled(true);


        wv.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                    if(flag)
                    {
                            Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "if", 1000);
                            toast1.show();
                            upperdock.bringToFront();
                            tocparent.bringToFront();
                            tocbottom.bringToFront();
                            upperdock.setVisibility(RelativeLayout.VISIBLE);
                            tocparent.setVisibility(LinearLayout.VISIBLE);
                            tocbottom.setVisibility(LinearLayout.VISIBLE);
                            flag=false;
                    }
                    else
                    {
                            Toast toast2 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "else", 1000);
                            toast2.show();
                            wv.bringToFront();
                            upperdock.setVisibility(RelativeLayout.INVISIBLE);
                            tocparent.setVisibility(LinearLayout.INVISIBLE);
                            tocbottom.setVisibility(LinearLayout.INVISIBLE);
                            flag=true;  

                    }

                    return flag;
            }
        });
    }
   }

【问题讨论】:

  • 你怎么知道它正在执行我和其他人......通过调试或通过 toast2?
  • @Dheeresh Singh 吐司。
  • 那你可以试试hotveryspicy的回答,然后在Dhruv的回答中找到原因

标签: android touch-event


【解决方案1】:

因为OnTouchListener不断调用,所以你需要通过MotionEvent.ACTION_DOWN检查用户第一次触摸

wv.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction()== MotionEvent.ACTION_DOWN)
        {
                if(flag)
                {
                   Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "if", 1000);
                   toast1.show();
                   upperdock.bringToFront();
                   tocparent.bringToFront();
                   tocbottom.bringToFront();
                   upperdock.setVisibility(RelativeLayout.VISIBLE);
                   tocparent.setVisibility(LinearLayout.VISIBLE);
                   tocbottom.setVisibility(LinearLayout.VISIBLE);
                   flag=false;
                }
                else
                {
                   Toast toast2 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "else", 1000);
                   toast2.show();
                   wv.bringToFront();
                   upperdock.setVisibility(RelativeLayout.INVISIBLE);
                   tocparent.setVisibility(LinearLayout.INVISIBLE);
                   tocbottom.setVisibility(LinearLayout.INVISIBLE);
                   flag=true;
                }
          }
              return flag;
        }
    });

【讨论】:

  • 我试过了,它给出了这个错误..(不兼容的操作数类型 MotionEvent 和 int)有什么建议可以清除它吗?我是安卓新手
  • stackoverflow.com/questions/6559881/… 通过此链接找到答案 感谢您的帮助
  • 我忘了写 getAction()。 :)
  • 你对切换外观有什么想法吗..这个答案帮助我解决了触摸效果..但是如何切换呢?在交替点击时,它必须切换外观
【解决方案2】:

触摸事件有很多动作,请在if条件下使用。

1.MotionEvent.ACTION_DOWN
2.MotionEvent.ACTION_MOVE
3.MotionEvent.ACTION_UP
.
.
.
etc

总是return false;

更新

if(event.getAction()== MotionEvent.ACTION_DOWN)
{
  //when you touch on screen
}
else if(event.getAction()== MotionEvent.ACTION_MOVE)
{
    //when you move your finger on screen
}
else if(event.getAction()== MotionEvent.ACTION_UP)
{
  //when you release your finger from screen
}

【讨论】:

  • 我试过了,它给出了这个错误..(不兼容的操作数类型 MotionEvent 和 int)有什么建议可以清除它吗?我是安卓新手——
  • stackoverflow.com/questions/6559881/… 通过此链接找到了答案 感谢您的帮助 -
  • 还是怎么切换dock的外观?切换是问题
  • 你的标准是什么,你想要什么??
【解决方案3】:

看起来只是两者都在同时执行。实际上,它们是分开执行的,但是因为触摸事件被触发得如此之快,所以它们似乎是同时执行的。你应该做的是首先检测运动事件的类型,例如MotionEvent.ACTION_MOVE 或其他东西。否则,所有运动事件都由您的侦听器代码处理。

当然,在您的代码中同时调用 ifelse 的原因是因为您在每个语句中将标志设置为 true 和 false,以便语句交替执行。

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多