如果都和else部分的TouchEvent执行 [英] TouchEvent executing both if and else part

查看:138
本文介绍了如果都和else部分的TouchEvent执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在I M面临的问题下面的code是在onTouchListener方法if和else部分两者是越来越每次执行。任何人有任何想法为什么会发生这样呢?和我的项目的目标是在触摸事件上的任何位置在船坞布局必须出现在另一个触摸它必须disappear.Can对此任何人帮助的WebView?

在此先感谢..

 公共类MathiasdockActivity延伸活动{
    / **当第一次创建活动调用。 * /
    RelativeLayout的upperdock,父母;
    的LinearLayout tocbottom,tocparent;
    布尔标志= TRUE;
    西弗吉尼亚州的WebView;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        父=(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(文件:///android_asset/hotspot/07-03.html);
        wv.getSettings()setJavaScriptEnabled(真)。
        wv.setOnTouchListener(新OnTouchListener(){            公共布尔onTouch(视图V,MotionEvent事件){                    如果(标志)
                    {
                            吐司toast1 = Toast.makeText(getBaseContext(),Boolean.toString(标志)+如果,1000);
                            toast1.show();
                            upperdock.bringToFront();
                            tocparent.bringToFront();
                            tocbottom.bringToFront();
                            upperdock.setVisibility(RelativeLayout.VISIBLE);
                            tocparent.setVisibility(LinearLayout.VISIBLE);
                            tocbottom.setVisibility(LinearLayout.VISIBLE);
                            标志= FALSE;
                    }
                    其他
                    {
                            吐司烤面包= Toast.makeText(getBaseContext(),Boolean.toString(标志)+其他,1000);
                            toast2.show();
                            wv.bringToFront();
                            upperdock.setVisibility(RelativeLayout.INVISIBLE);
                            tocparent.setVisibility(LinearLayout.INVISIBLE);
                            tocbottom.setVisibility(LinearLayout.INVISIBLE);
                            标志=真实的;                    }                    返回标志;
            }
        });
    }
   }


解决方案

其因 OnTouchListener 来电不断,所以你需要检查用户通过<$ C倒是第一次$ C> MotionEvent.ACTION_DOWN

  wv.setOnTouchListener(新OnTouchListener(){        公共布尔onTouch(视图V,MotionEvent事件){        如果(event.getAction()== MotionEvent.ACTION_DOWN)
        {
                如果(标志)
                {
                   吐司toast1 = Toast.makeText(getBaseContext(),Boolean.toString(标志)+如果,1000);
                   toast1.show();
                   upperdock.bringToFront();
                   tocparent.bringToFront();
                   tocbottom.bringToFront();
                   upperdock.setVisibility(RelativeLayout.VISIBLE);
                   tocparent.setVisibility(LinearLayout.VISIBLE);
                   tocbottom.setVisibility(LinearLayout.VISIBLE);
                   标志= FALSE;
                }
                其他
                {
                   吐司烤面包= Toast.makeText(getBaseContext(),Boolean.toString(标志)+其他,1000);
                   toast2.show();
                   wv.bringToFront();
                   upperdock.setVisibility(RelativeLayout.INVISIBLE);
                   tocparent.setVisibility(LinearLayout.INVISIBLE);
                   tocbottom.setVisibility(LinearLayout.INVISIBLE);
                   标志=真实的;
                }
          }
              返回标志;
        }
    });

In the code below the problem i m facing is that in onTouchListener method both if and else part is getting executed each time. Anyone have idea why is it happening like that? and aim of my project is on touch event on anywhere in on the webview the dock layout must appear on another touch it must disappear.Can anyone help regarding this?

Thanks in advance ..

     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;
            }
        });
    }
   }

解决方案

Its because OnTouchListener calls continuously, so you need to check that user touches first time by 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;
        }
    });

这篇关于如果都和else部分的TouchEvent执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆