android-TouchEvent执行if和else部分

android-TouchEvent执行if和else部分,第1张

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

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

以上是内存溢出为你收集整理的android-TouchEvent执行if和else部分全部内容,希望文章能够帮你解决android-TouchEvent执行if和else部分所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1073872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存