Android在web view中监听屏幕双击的两种方式onClick与onTouch
来源:互联网 发布:安卓扒谱软件 编辑:程序博客网 时间:2024/06/14 03:37
1.onclick方法
缺点:不能在web view打开的网页中监听
//MainActivity.java页面package com.example.aaa;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.ViewGroup;import com.example.aaa.MainActivity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;import android.widget.Toast;import android.view.LayoutInflater;import android.widget.LinearLayout;public class MainActivity extends ActionBarActivity { private boolean waitDouble = true; private static final int DOUBLE_CLICK_TIME = 350; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); //TextView hello = (TextView)findViewById(R.id.q); LayoutInflater inflater = LayoutInflater.from(MainActivity.this); LinearLayout hello = (LinearLayout)findViewById(R.id.q); //LinearLayout hello = (LinearLayout)findViewById(R.id.q); hello.setOnClickListener(listener); } OnClickListener listener = new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub if(waitDouble == true){ waitDouble = false; Thread thread = new Thread(){ @Override public void run(){ try { sleep(DOUBLE_CLICK_TIME); if(waitDouble == false){ waitDouble = true; singleClick(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; thread.start(); }else{ waitDouble = true; doubleClick(); } } }; private void singleClick(){ System.out.println("single"); } private void doubleClick(){ Toast.mak``Text(MainActivity.this, "双击", Toast.LENGTH_LONG) .show(); }}
//activity_main.xml页面<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/q" android:descendantFocusability="blocksDescendants" android:orientation="vertical" > <TextView android:id="@+id/c" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello" /></LinearLayout>
源码下载:http://download.csdn.net/download/qq_25252769/9958627
2.onTouch方法
可以在web view打开的页面使用双击事件
//MainActivity.java页面package com.example.full;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import com.example.full.R;import com.example.full.MainActivity;import android.app.Activity;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.TextView;import android.widget.Toast;import android.view.LayoutInflater;import android.widget.LinearLayout; import android.app.ActivityManager;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.view.KeyEvent;import android.webkit.WebView;import android.webkit.WebViewClient;import android.view.Window;import android.view.MotionEvent;public class MainActivity extends ActionBarActivity{ private boolean waitDouble = true; private static final int DOUBLE_CLICK_TIME = 350; private final int DOUBLE_TAP_TIMEOUT = 200; private MotionEvent mCurrentDownEvent; private MotionEvent mPreviousUpEvent; public void onCreate(Bundle savedInstanceState){ requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //外部网页 init(); //TextView hello = (TextView)findViewById(R.id.q); //LayoutInflater inflater = LayoutInflater.from(MainActivity.this); //LinearLayout hello = (LinearLayout)findViewById(R.id.q); //LinearLayout hello = (LinearLayout)findViewById(R.id.q); //WebView hello = (WebView)findViewById(R.id.webView); webView = (WebView) findViewById(R.id.webView); webView.setOnTouchListener(listener); } OnTouchListener listener = new OnTouchListener(){ @Override public boolean onTouch(View v,MotionEvent event) { // TODO Auto-generated method stub /*if(waitDouble == true){ waitDouble = false; Thread thread = new Thread(){ @Override public void run(){ try { sleep(DOUBLE_CLICK_TIME); if(waitDouble == false){ waitDouble = true; singleClick(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; thread.start(); }else{ waitDouble = true; doubleClick(); }*/ if (event.getAction() == MotionEvent.ACTION_DOWN) { if (mPreviousUpEvent != null && mCurrentDownEvent != null && isConsideredDoubleTap(mCurrentDownEvent, mPreviousUpEvent, event)) { Toast.makeText(MainActivity.this, "双击", Toast.LENGTH_LONG).show(); } mCurrentDownEvent = MotionEvent.obtain(event); } else if (event.getAction() == MotionEvent.ACTION_UP) { mPreviousUpEvent = MotionEvent.obtain(event); } return false; } }; private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent firstUp, MotionEvent secondDown) { if (secondDown.getEventTime() - firstUp.getEventTime() > DOUBLE_TAP_TIMEOUT) { return false; } int deltaX = (int) firstUp.getX() - (int) secondDown.getX(); int deltaY = (int) firstUp.getY() - (int) secondDown.getY(); return deltaX * deltaX + deltaY * deltaY < 10000; } private void singleClick(){ Toast.makeText(MainActivity.this, "单击", Toast.LENGTH_LONG) .show(); } private void doubleClick(){ Toast.makeText(MainActivity.this, "双击", Toast.LENGTH_LONG).show(); } private WebView webView; private void init(){ webView = (WebView) findViewById(R.id.webView); webView.getSettings().setBuiltInZoomControls(true); //WebView加载web资源 webView.loadUrl("http://baidu.com"); //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //返`值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return true; } }); }}
//activity_main.xml页面<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:fadingEdgeLength="0sp" android:id="@+id/q" > <LinearLayout android:id="@+id/myTitleBarLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/myTitleBarTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/app_name" android:paddingTop="4dip" android:paddingBottom="4dip" android:paddingLeft="6dip" android:textStyle="bold" android:shadowColor="#BB000000" android:shadowRadius="3.0" android:shadowDy=".25" /> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="#CCEEEEEE" android:padding="10dip" /> </LinearLayout> <!-- <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > Insert your regular layout stuff here <Button android:id="@+id/toggle_title_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Toggle Title" /> </ScrollView> --> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" /></LinearLayout>
源码下载:http://download.csdn.net/download/qq_25252769/9958637
阅读全文
0 0
- Android在web view中监听屏幕双击的两种方式onClick与onTouch
- Android中onTouch与onClick两种监听的完全解析
- 关于Android中onTouch时间与onClick事件的监听问题
- Android中view的onTouch&onClick事件分发机制详解
- android view 的onTouch 和 onClick 事件
- Android中onTouch与onClick事件的关系
- Android中onTouch与onClick事件的关系
- android onClick 与 onTouch 的区别
- Android onTouch 与 onClick 的关系
- view的触摸事件OnTouch与监听
- android onclick与ontouch和scroll监听冲突的解决方法 类似QQHD拖动窗口效果
- Android 中 onTouch 和OnClick 冲突的处理(onTouchEvent返回true时与onclick冲突)
- ontouch与onclick共存情况的view响应问题
- Android View的onTouch和onClick和onLongClick事件
- Android View的onTouch、onClick和onLongClick事件分析
- Android View的onTouch和onClick和onLongClick事件
- onTouch 事件与onClick事件发生冲突,如何在onTouch事件中实现点击事件
- 【Android】 onClick与onTouch并存触发的问题
- mediawiki常用设置
- js 中的call 和 apply
- 激活函数、损失函数与超参数
- 推荐-高质量C++/C编程指南(林锐)
- 前端技巧:文本框输入字母变为大写
- Android在web view中监听屏幕双击的两种方式onClick与onTouch
- FileProvider无法获取外置SD卡问题解决方案 | Failed to find configured root that contains
- myeclipse 注释和代码格式化
- Hadoop学习线路指导
- Android自定义View 之attr
- 早间简评:黄金亚盘快速下跌 1300关口岌岌可危?
- hbase安装配置(整合到hadoop)
- Spring MVC中常用注解之RequestMapping详解
- Mycat 数据库分库分表中间件