使用音量键滚动 webView

发布于 2024-10-16 09:34:53 字数 1044 浏览 4 评论 0原文

如何使用音量硬键滚动 webView? ...可以通过缓动来完成吗?如果是这样,怎么办?我是 Android 新手 - 来自 ActionScript,任何帮助将不胜感激。

my R.id.webPg001 is a WebView id.

这就是我现在所处的位置:

@Override
  public boolean dispatchKeyEvent(KeyEvent event) {
      int action = event.getAction();
      int keyCode = event.getKeyCode();
      ScrollView scrollView;
      scrollView = (ScrollView) findViewById(R.id.webPg001);        
          switch (keyCode) {
          case KeyEvent.KEYCODE_VOLUME_UP:
              if (action == KeyEvent.ACTION_UP) {
                  scrollView.pageScroll(ScrollView.FOCUS_UP);
                  scrollView.computeScroll(); 
              }
              return true;
          case KeyEvent.KEYCODE_VOLUME_DOWN:
              if (action == KeyEvent.ACTION_UP) {
          scrollView.pageScroll(ScrollView.FOCUS_DOWN);
                  scrollView.computeScroll(); 
              }
              return true;
          default:
              return super.dispatchKeyEvent(event);
          }
      }

How do you scroll a webView w/ the volume hard keys? ...and can it be done with easing? If so, how? I'm a nooB to Android - Coming over from ActionScript and any help will be greatly appreciated.

my R.id.webPg001 is a WebView id.

This is where I'm at now:

@Override
  public boolean dispatchKeyEvent(KeyEvent event) {
      int action = event.getAction();
      int keyCode = event.getKeyCode();
      ScrollView scrollView;
      scrollView = (ScrollView) findViewById(R.id.webPg001);        
          switch (keyCode) {
          case KeyEvent.KEYCODE_VOLUME_UP:
              if (action == KeyEvent.ACTION_UP) {
                  scrollView.pageScroll(ScrollView.FOCUS_UP);
                  scrollView.computeScroll(); 
              }
              return true;
          case KeyEvent.KEYCODE_VOLUME_DOWN:
              if (action == KeyEvent.ACTION_UP) {
          scrollView.pageScroll(ScrollView.FOCUS_DOWN);
                  scrollView.computeScroll(); 
              }
              return true;
          default:
              return super.dispatchKeyEvent(event);
          }
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是我的挚爱i 2024-10-23 09:34:53

这是正确的代码:(thnx NdrU!!)

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    WebView scrollView = (WebView) findViewById(R.id.ch01);     
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_DOWN) {
                scrollView.pageUp(false);   
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                scrollView.pageDown(false);
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    } 

Here is the correct code: (thnx NdrU!!)

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    WebView scrollView = (WebView) findViewById(R.id.ch01);     
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_DOWN) {
                scrollView.pageUp(false);   
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                scrollView.pageDown(false);
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    } 
青萝楚歌 2024-10-23 09:34:53

WebView 有方法 pageUppageDown,根据 javadoc,它们滚动半页,因此您可能需要调用它们两次以获得整页滚动。

至于您拥有的代码,我认为监听按钮按下的首选方法是覆盖 onKeyUponKeyDown 方法,但我可能是错的(我是新手我自己开发android)。

WebView has methods pageUp and pageDown, according to the javadoc, they scroll half a page, so you will probably want to call them twice to get a full page scroll.

As to the code you have, I thought the preffered way to listen to button presses is by overriding onKeyUp and onKeyDown methods, but I might be wrong (I'm new to android development myself).

西瑶 2024-10-23 09:34:53

以防万一您不知道,正常的导航机制是使用箭头键或轨迹球进行硬件级导航。您的网络视图应该插入到此机制中,而无需您付出任何额外的努力,因此,除非您有明确的应用程序特定原因需要点击音量键,否则我建议您使用此处的内置功能。

Just in case you're not aware, the normal mechanism for navigation is to use the arrow keys or trackball for hardware-level navigation. Your webviews should plug in to this mechanism with no additional effort on your part, and so unless you have a clear application-specific reason for tapping into the volume keys, I would recommend using the built-in functionality here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文