Android:让WebView支持元素

原文地址:http://www.tuicool.com/articles/buu6ji

在Android中,当我们通过WebView打开一个页面时,如果里面有元素是类型的,WebView只能正常的显示样式,但是是无法点击的。要解决这个问题,我们需要重写WebChromeClient,以及onActivityResult()方法

下面直接给出Demo代码:

Activity文件主要代码:

 web.setWebChromeClient(new WebChromeClient() {
      //关键代码,以下函数是没有API文档的,所以在Eclipse中会报错,如果添加了@Override关键字在这里的话。

      // For Android 3.0+
      public void openFileChooser(ValueCallback uploadMsg) {

        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

      }

      // For Android 3.0+
      public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        MainActivity.this.startActivityForResult(
            Intent.createChooser(i, "File Browser"),
            FILECHOOSER_RESULTCODE);
      }

      //For Android 4.1
      public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);

      }
    });


@Override
protected void onActivityResult(int requestCode, int resultCode,
                  Intent intent) {
   if (requestCode == FILECHOOSER_RESULTCODE) {
     if (null == mUploadMessage) return;
     Uri result = intent == null || resultCode != RESULT_OK ? null
         : intent.getData();
     mUploadMessage.onReceiveValue(result);
     mUploadMessage = null;
   }
}

你可能感兴趣的:(android开发)