我想知道用户何时单击Webview而不单击超链接。在该单击上,我想显示/隐藏我的活动视图,该视图包含一个Webview。有什么建议吗?
我想知道用户何时单击Webview而不单击超链接。在该单击上,我想显示/隐藏我的活动视图,该视图包含一个Webview。有什么建议吗?
Answers:
我对此进行了查看,发现aWebView
似乎没有将点击事件发送到OnClickListener
。如果外面有人可以证明我错了或告诉我原因,那么我很想听听。
我的发现是,aWebView
会将触摸事件发送到OnTouchListener
。它确实有自己的onTouchEvent
方法,但我似乎从未MotionEvent.ACTION_MOVE
使用过这种方法。
因此,鉴于我们可以在注册的触摸事件侦听器上获取事件,剩下的唯一问题是如何规避用户单击URL时要为触摸执行的任何操作。
Handler
通过发送一些延迟的触摸消息,然后如果触摸是由用户单击URL引起的,则删除那些触摸消息,可以通过一些花哨的步法来实现。
这是一个例子:
public class WebViewClicker extends Activity implements OnTouchListener, Handler.Callback {
private static final int CLICK_ON_WEBVIEW = 1;
private static final int CLICK_ON_URL = 2;
private final Handler handler = new Handler(this);
private WebView webView;
private WebViewClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view_clicker);
webView = (WebView)findViewById(R.id.web);
webView.setOnTouchListener(this);
client = new WebViewClient(){
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
handler.sendEmptyMessage(CLICK_ON_URL);
return false;
}
};
webView.setWebViewClient(client);
webView.setVerticalScrollBarEnabled(false);
webView.loadUrl("http://www.example.com");
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.web && event.getAction() == MotionEvent.ACTION_DOWN){
handler.sendEmptyMessageDelayed(CLICK_ON_WEBVIEW, 500);
}
return false;
}
@Override
public boolean handleMessage(Message msg) {
if (msg.what == CLICK_ON_URL){
handler.removeMessages(CLICK_ON_WEBVIEW);
return true;
}
if (msg.what == CLICK_ON_WEBVIEW){
Toast.makeText(this, "WebView clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
希望这可以帮助。
您可以通过OnTouchListener实现OnClickListener:
webView.setOnTouchListener(new View.OnTouchListener() {
public final static int FINGER_RELEASED = 0;
public final static int FINGER_TOUCHED = 1;
public final static int FINGER_DRAGGING = 2;
public final static int FINGER_UNDEFINED = 3;
private int fingerState = FINGER_RELEASED;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
if (fingerState == FINGER_RELEASED) fingerState = FINGER_TOUCHED;
else fingerState = FINGER_UNDEFINED;
break;
case MotionEvent.ACTION_UP:
if(fingerState != FINGER_DRAGGING) {
fingerState = FINGER_RELEASED;
// Your onClick codes
}
else if (fingerState == FINGER_DRAGGING) fingerState = FINGER_RELEASED;
else fingerState = FINGER_UNDEFINED;
break;
case MotionEvent.ACTION_MOVE:
if (fingerState == FINGER_TOUCHED || fingerState == FINGER_DRAGGING) fingerState = FINGER_DRAGGING;
else fingerState = FINGER_UNDEFINED;
break;
default:
fingerState = FINGER_UNDEFINED;
}
return false;
}
});
Hamidreza的解决方案几乎对我有用。
我从实验中注意到,一个简单的点击通常会产生2-5个动作事件。检查动作上下之间的时间更简单,并且行为更像我期望的那样。
private class CheckForClickTouchLister implements View.OnTouchListener {
private final static long MAX_TOUCH_DURATION = 100;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
m_DownTime = event.getEventTime(); //init time
break;
case MotionEvent.ACTION_UP:
if(event.getEventTime() - m_DownTime <= MAX_TOUCH_DURATION)
//On click action
break;
default:
break; //No-Op
}
return false;
}
On Click事件On webView在onTouch中的工作方式如下:
imagewebViewNewsChart.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_MOVE){
return false;
}
if (event.getAction()==MotionEvent.ACTION_UP){
startActivity(new Intent(this,Example.class));
}
return false;
}
});
我认为您也可以使用MotionEvent.ACTION_UP状态,例如:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.iv:
if (event.getAction() == MotionEvent.ACTION_UP) {
if (isheadAndFootShow == false) {
headLayout.setVisibility(View.VISIBLE);
footLayout.setVisibility(View.VISIBLE);
isheadAndFootShow = true;
} else {
headLayout.setVisibility(View.INVISIBLE);
footLayout.setVisibility(View.INVISIBLE);
isheadAndFootShow = false;
}
return true;
}
break;
}
return false;
}
这对我有用
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Do what you want
return false;
}
});
我已经尝试了几乎所有答案,其中一些几乎对我有用,但是这些答案都无法完美解决。所以这是我的解决方案。对于此解决方案,您将需要onclick
在HTML body标签中添加属性。因此,如果您无法修改HTML文件,那么该解决方案将无法为您服务。
步骤1:onclick
在HTML正文标签中添加属性,如下所示:
<body onclick="ok.performClick(this.value);">
<h1>This is heading 1</h1>
<p>This is para 1</p>
</body>
步骤2:addJavascriptInterface
在您的应用中实施以处理点击侦听器:
webView.addJavascriptInterface(new Object() {
@JavascriptInterface
public void performClick(String string) {
handler.sendEmptyMessageDelayed(CLICK_ON_WEBVIEW, 0);
}
}, "ok");
步骤3:我们无法对performClick
方法中的主线程执行任何操作,因此需要添加Handler
类:
private final Handler handler = new Handler(this); // class-level variable
private static final int CLICK_ON_WEBVIEW = 1; // class-level variable
实施Handler.Callback
到您的班级和替代handleMessage
@Override
public boolean handleMessage(Message message) {
if (message.what == CLICK_ON_WEBVIEW) {
Toast.makeText(getActivity(), "WebView clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}