我想通过点击屏幕在用户onClickListener
时获得点击事件。我的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout_content">
<WebView
android:id="@+id/webview_item"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
我试图通过布局Relative setOnClickListener:
来做到这一点RelativeLayout layoutContent = (RelativeLayout) findViewById(R.id.layout_content);
layoutContent.setOnClickListenner(new OnClickListener() {
@Override
public void onClick(View v) {
......
}
});
我可以获得良好的点击事件,直到webview完成加载,我再也无法获得clicklistener了。那么有人建议我在加载webview后能够获得onClicklistener吗?
答案 0 :(得分:1)
根据this回答,
WebView似乎没有将点击事件发送到OnClickListener
为此,您需要实施onTouchListener
事件。
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;
}
}
答案 1 :(得分:0)
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Intent intent;
ActivityModel activityModel = new ActivityModel();
activityModel.setServerId(activityId);
if (action.equals("discussion")) {
intent = new Intent(BannerPopUp.this, DiscussionActivity.class);
intent.putExtra("activityModel", activityModel);
} else {
intent = new Intent(BannerPopUp.this, DisplayActivity.class);
intent.putExtra(Constants.MODEL_ACTIVITY, activityModel);
if (action.equals("join")) {
intent.putExtra(Constants.ACTION, "join");
}
}
if (!isAppRunning) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
finish();
return false;`
}
});