Android WebView Touch事件及相关问题处理

如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456


继上一篇 Android WebView常见问题及解决方案汇总 中归纳了一些处理webview的常见问题,这次要说的是webview中的touch事件:

有时候在开发中,我们需要对webview加入触摸事件的处理,比如加入滑动效果或者类似于阅读中的翻页效果,这时候我们就需要重写webview中的onTouch方法:

public class MyWebView extends WebView{
public MyWebView(Context context) {
  super(context);

  }

@Override
public boolean onTouchEvent(MotionEvent evt) {
  
   switch (evt.getAction()){
   
   case MotionEvent.ACTION_DOWN:
    //do something...... 
    break;
    
   case MotionEvent.ACTION_MOVE:
   
    //do something...... 
    break;          case MotionEvent.ACTION_UP:     
    //do something...... 
               
     break;               
    }                 
    return false;}
}



这里要注意的是,返回值要为false,将此事件继续向下传递.否则会引起 超链接不起作用的问题.


将onTouch()事件中的返回值改为false之后,有时候仍然还是不会触发onTouch()事件,这是为什么呢.经过逐行代码的分析,终于找到了问题的根源:

主要是因为做了如下设置:

webView.getSettings().setBuiltInZoomControls(true);

该设置让webview控件可以支持缩放,而这个缩放设置,同样会响应onTouch事件的,所以会覆盖掉我们自己设置的onTouhc监听,引起了事件的冲突.

怎么办呢,如果需要设置webview大小的话,不是用webview的setting支持,而是调用webView.setInitialScale()函数来设置webview的大小

具体的数值可以这样处理:

1.如果webview需要固定大小,可根据分辨率及webview位置综合计算页面大小初始比例.当然一般遇到页面大小固定的话,还是在H5页面中进行适配处理最好

2.如果就是需要对webview进行缩放,则需要给用户提供一个缩放按钮,然后自己控制每次缩放的数值,然后调用webView.setInitialScale(),数值大小根据需求而定,若无要求可参照webview本身支持的缩放大小数值.










  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Title: Android Programming: The Big Nerd Ranch Guide, 3rd Edition Author: Bill Phillips, Chris Stewart Length: 624 pages Edition: 3 Language: English Publisher: Big Nerd Ranch Guides Publication Date: 2017-02-09 ISBN-10: 0134706056 ISBN-13: 9780134706054 Table of Contents Chapter 1. Your First Android Application Chapter 2. Android and Model-View-Controller Chapter 3. The Activity Lifecycle Chapter 4. Debugging Android Apps Chapter 5. Your Second Activity Chapter 6. Android SDK Versions and Compatibility Chapter 7. UI Fragments and the Fragment Manager Chapter 8. Displaying Lists with RecyclerView Chapter 9. Creating User Interfaces with Layouts and Widgets Chapter 10. Using Fragment Arguments Chapter 11. Using ViewPager Chapter 12. Dialogs Chapter 13. The Toolbar Chapter 14. SQLite Databases Chapter 15. Implicit Intents Chapter 16. Taking Pictures with Intents Chapter 17. Two-Pane Master-Detail Interfaces Chapter 18. Localization Chapter 19. Accessibility Chapter 20. Data Binding and MVVM Chapter 21. Unit Testing and Audio Playback Chapter 22. Styles and Themes Chapter 23. XML Drawables Chapter 24. More About Intents and Tasks Chapter 25. HTTP and Background Tasks Chapter 26. Loopers, Handlers, and HandlerThread Chapter 27. Search Chapter 28. Background Services Chapter 29. Broadcast Intents Chapter 30. Browsing the Web and WebView Chapter 31. Custom Views and Touch Events Chapter 32. Property Animation Chapter 33. Locations and Play Services Chapter 34. Maps Chapter 35. Material Design Chapter 36. Afterword
1. Introduction 1.1 Document Structure 1.1.1. Requirements by Device Type 1.1.2. Requirement ID 1.1.3. Requirement ID in Section 2 2. Device Types 2.1 Device Configurations 2.2. Handheld Requirements 2.2.1. Hardware 2.2.2. Multimedia 2.2.3. Software 2.2.4. Performance and Power 2.2.5. Security Model 2.3. Television Requirements 2.3.1. Hardware 2.3.2. Multimedia 2.3.3. Software 2.2.4. Performance and Power 2.4. Watch Requirements 2.4.1. Hardware 2.4.2. Multimedia 2.4.3. Software 2.5. Automotive Requirements 2.5.1. Hardware 2.5.2. Multimedia 2.5.3. Software 2.2.4. Performance and Power 2.2.5. Security Model 2.6. Tablet Requirements 2.4.1. Hardware 3. Software 3.1. Managed API Compatibility 3.1.1. Android Extensions 3.2. Soft API Compatibility 3.2.1. Permissions 3.2.2. Build Parameters 3.2.3. Intent Compatibility 3.2.3.1. Core Application Intents 3.2.3.2. Intent Resolution 3.2.3.3. Intent Namespaces 3.2.3.4. Broadcast Intents 3.2.3.5. Default App Settings 3.2.4. Activities on secondary displays 3.3. Native API Compatibility 3.3.1. Application Binary Interfaces 3.3.2. 32-bit ARM Native Code Compatibility 3.4. Web Compatibility 3.4.1. WebView Compatibility 3.4.2. Browser Compatibility 3.5. API Behavioral Compatibility 3.6. API Namespaces 3.7. Runtime Compatibility 3.8. User Interface Compatibility 3.8.1. Launcher (Home Screen) 3.8.2. Widgets 3.8.3. Notifications 3.8.3.1. Presentation of Notifications 3.8.3.2. Notification Listener Service 3.8.3.3. DND (Do not Disturb) 3.8.4. Search 3.8.5. Alerts and Toasts 3.8.6. Themes 3.8.7. Live Wallpapers 3.8.8. Activity Switching 3.8.9. Input Management 3.8.10. Lock Screen Media Control 3.8.11. Screen savers (previously Dreams) 3.8.12. Location 3.8.13. Unicode and Font 3.8.14. Multi-windows 3.9. Device Administration 3.9.1 Device Provisioning 3.9.1.1 Device owner provisioning 3.9.1.2 Managed profile provisioning 3.9.2 Managed Profile Support 3.10. Accessibility 3.11. Text-to-Speech 3.12. TV Input Framework 3.12.1. TV App 3.12.1.1. Electronic Program Guide 3.12.1.2. Navigation 3.12.1.3. TV input app linking 3.12.1.4. Time shifting 3.12.1.5. TV recording 3.13. Quick Settings 3.14. Media UI 3.15. Instant Apps 3.16. Companion Device Pairing 4. Application Packaging Compatibility 5. Multimedia Compatibility 5.1. Media Codecs 5.1.1. Audio Encoding 5.1.2. Audio Decoding 5.1.3. Audio Codecs Details 5.1.4. Image Encoding 5.1.5. Image Decoding 5.1.6. Image Codecs Details 5.1.7. Video Codecs 5.1.8. Video Codecs List 5.2. Video Encoding 5.2.1. H.263 5.2.2. H-264 5.2.3. VP8 5.2.4. VP9 5.3. Video Decoding 5.3.1. MPEG-2 5.3.2. H.263 5.3.3. MPEG-4 5.3.4. H.264 5.3.5. H.265 (HEVC) 5.3.6. VP8 5.3.7. VP9 5.4. Audio Recording 5.4.1. Raw Audio Capture 5.4.2. Capture for Voice Recognition 5.4.3. Capture for Rerouting of Playback 5.5. Audio Playback 5.5.1. Raw Audio Playback 5.5.2. Audio Effects 5.5.3. Audio Output Volume 5.6. Audio Latency 5.7. Network Protocols 5.8. Secure Media 5.9. Musical Instrument Digital Interface (MIDI) 5.10. Professional Audio 5.11. Capture for Unprocessed 6. Developer Tools and Options Compatibility 6.1. Developer Tools 6.2. Developer Options 7. Hardware Compatibility 7.1. Display and Graphics 7.1.1. Screen Configuration 7.1.1.1. Screen Size 7.1.1.2. Screen Aspect Ratio 7.1.1.3. Screen Density 7.1.2. Display Metrics 7.1.3. Screen Orientation 7.1.4. 2D and 3D Graphics Acceleration 7.1.4.1 OpenGL ES 7.1.4.2 Vulkan 7.1.4.3 RenderScript 7.1.4.4 2D Graphics Acceleration 7.1.4.5 Wide-gamut Displays 7.1.5. Legacy Application Compatibility Mode 7.1.6. Screen Technology 7.1.7. Secondary Displays 7.2. Input Devices 7.2.1. Keyboard 7.2.2. Non-touch Navigation 7.2.3. Navigation Keys 7.2.4. Touchscreen Input 7.2.5. Fake Touch Input 7.2.6. Game Controller Support 7.2.6.1. Button Mappings 7.2.7. Remote Control 7.3. Sensors 7.3.1. Accelerometer 7.3.2. Magnetometer 7.3.3. GPS 7.3.4. Gyroscope 7.3.5. Barometer 7.3.6. Thermometer 7.3.7. Photometer 7.3.8. Proximity Sensor 7.3.9. High Fidelity Sensors 7.3.10. Fingerprint Sensor 7.3.11. Android Automotive-only sensors 7.3.11.1. Current Gear 7.3.11.2. Day Night Mode 7.3.11.3. Driving Status 7.3.11.4. Wheel Speed 7.3.12. Pose Sensor 7.4. Data Connectivity 7.4.1. Telephony 7.4.1.1. Number Blocking Compatibility 7.4.2. IEEE 802.11 (Wi-Fi) 7.4.2.1. Wi-Fi Direct 7.4.2.2. Wi-Fi Tunneled Direct Link Setup 7.4.2.3. Wi-Fi Aware 7.4.2.4. Wi-Fi Passpoint 7.4.3. Bluetooth 7.4.4. Near-Field Communications 7.4.5. Minimum Network Capability 7.4.6. Sync Settings 7.4.7. Data Saver 7.5. Cameras 7.5.1. Rear-Facing Camera 7.5.2. Front-Facing Camera 7.5.3. External Camera 7.5.4. Camera API Behavior 7.5.5. Camera Orientation 7.6. Memory and Storage 7.6.1. Minimum Memory and Storage 7.6.2. Application Shared Storage 7.6.3. Adoptable Storage 7.7. USB 7.7.1. USB peripheral mode 7.7.2. USB host mode 7.8. Audio 7.8.1. Microphone 7.8.2. Audio Output 7.8.2.1. Analog Audio Ports 7.8.3. Near-Ultrasound 7.9. Virtual Reality 7.9.1. Virtual Reality Mode 7.9.2. Virtual Reality High Performance 8. Performance and Power 8.1. User Experience Consistency 8.2. File I/O Access Performance 8.3. Power-Saving Modes 8.4. Power Consumption Accounting 8.5. Consistent Performance 9. Security Model Compatibility 9.1. Permissions 9.2. UID and Process Isolation 9.3. Filesystem Permissions 9.4. Alternate Execution Environments 9.5. Multi-User Support 9.6. Premium SMS Warning 9.7. Kernel Security Features 9.8. Privacy 9.8.1. Usage History 9.8.2. Recording 9.8.3. Connectivity 9.8.4. Network Traffic 9.9. Data Storage Encryption 9.9.1. Direct Boot 9.9.2. File Based Encryption 9.9.3. Full Disk Encryption 9.10. Device Integrity 9.11. Keys and Credentials 9.11.1. Secure Lock Screen 9.12. Data Deletion 9.13. Safe Boot Mode 9.14. Automotive Vehicle System Isolation 10. Software Compatibility Testing 10.1. Compatibility Test Suite 10.2. CTS Verifier 11. Updatable Software 12. Document Changelog 12.1. Changelog Viewing Tips 13. Contact Us
Android WebView中实现图片点击事件,主要通过以下步骤: 1. 首先,需要注入JavaScript代码到WebView中,以便响应图片的点击事件。可以使用WebView的addJavascriptInterface()方法将一个Java对象注入到WebView中,这个Java对象可以作为JavaScript对象在WebView中调用。 2. 创建一个自定义的Java类,例如ImageJavascriptInterface,该类包含一个openImage()方法,用于处理图片点击事件。在openImage()方法中,可以执行跳转到图片查看页面等操作。 3. 在WebViewWebViewClient中的onPageFinished()方法中,通过执行JavaScript代码,为每个图片添加点击事件监听器,并调用注入的Java对象的openImage()方法。这样当用户点击图片时,会触发openImage()方法,并传递图片的URL和位置信息。 4. 最后,将HTML数据加载到WebView中,可以使用loadData()方法。 综上所述,通过注入JavaScript代码和自定义的Java对象,可以实现在Android WebView中响应图片点击事件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [android webview js交互, 响应webview中的图片点击事件](https://download.csdn.net/download/zlb_lover/9654404)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android webview中的图片点击事件](https://blog.csdn.net/weixin_42273922/article/details/106781799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值