美文网首页
Android在web view中监听屏幕双击的两种方式onCl

Android在web view中监听屏幕双击的两种方式onCl

作者: 刘白超 | 来源:发表于2020-02-24 17:21 被阅读0次

    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

    相关文章

      网友评论

          本文标题:Android在web view中监听屏幕双击的两种方式onCl

          本文链接:https://www.haomeiwen.com/subject/dfsbqhtx.html