在下面的代码中我遇到的问题是在onTouchListener方法中,if和else部分每次都被执行。任何人都知道为什么会这样发生?我的项目的目标是在webview上的任何地方进行触摸事件,底座布局必须出现在另一个触摸上它必须消失。任何人都可以帮忙解决这个问题吗?
提前致谢..
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;
}
});
}
}
答案 0 :(得分: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;
}
});
答案 1 :(得分:1)
它们看起来似乎都在同时执行。实际上,它们是分开执行的,但是因为触摸事件被如此快速地触发,看起来它们都是同时执行的。您应该做的是首先检测运动事件的类型,例如MotionEvent.ACTION_MOVE
或其他东西。否则,所有运动事件都由您的侦听器代码处理。
当然,在您的代码中调用if
和else
的原因是因为您在每个语句中将标志设置为true和false,以便语句的执行交替。 / p>
答案 2 :(得分:1)
触摸事件有很多动作,所以请在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
}