其他分享
首页 > 其他分享> > android-TouchEvent执行if和else部分

android-TouchEvent执行if和else部分

作者:互联网

在下面的代码中,我面临的问题是在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;
            }
        });
    }
   }

解决方法:

这是因为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;
        }
    });

标签:touch-event,android
来源: https://codeday.me/bug/20191201/2079554.html