【问题标题】:Cordova WebView in a Dialog对话框中的 Cordova WebView
【发布时间】:2014-03-24 10:11:12
【问题描述】:

我可以在 android 的 CustomDialog 中实现 Cordova WebView 吗? 我想单击一个按钮,这会显示一个带有 webview 的对话框。我用这种方法试过了,但是没有用。

    button = (Button) findViewById(R.id.buttonShowCustomDialog);

        // add button click listener
        button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");
            cwv = (CordovaWebView) findViewById(R.id.webview);
            Config.init(this);
            cwv.loadUrl("file:///android_asset/www/index.html");
            dialog.show();
    }
});

【问题讨论】:

    标签: android cordova webview dialog


    【解决方案1】:

    更新 2:

    其实扩展的Dialog并不需要实现CordovaInterface。只需要重写setContentView,就够了。

    public class CordovaDialog extends Dialog {
        private Context currentContext;
    
        public CordovaDialog(Context context) {
            super(context);
            this.currentContext = context;
        }
    
        // we have to override this because we need to disable attaching to root when inflating (wtf cordova ??) 
        @Override public void setContentView(int layoutResID) {
            final LayoutInflater inflater = LayoutInflater.from(this.currentContext);
            View v = inflater.inflate(layoutResID, null, false);
            super.setContentView(v);
        };
    
    }
    

    我也有同样的问题。我还尝试创建一个扩展 Dialog 并实现 CordovaInterface 的类,但也没有任何运气。似乎每次我调用 setContentView 时,Cordova 都找不到与 Dialog 关联的 Activity,并且 logcat 显示警告说我的 Activity 没有实现 CordovaInterface 但它确实实现了。

    更新: 好的,我想通了。所以这就是我的做法。它很长,但很有效。

    1. 首先,让我们假设创建对话框的父 Activity 已经在实现 CordovaInterface。另外,假设您的 CordovaWebview 在布局内。
    2. 创建一个扩展 Dialog 并实现 CordovaInterface 的新类(例如 CordovaDialog)。
    3. 为传递上下文和接口的 CordovaDialog 类创建一个新的构造函数,以便您可以从父活动设置 CordovaInterface(它也应该实现 CordovaInterface)。
    4. 覆盖 CordovaDialog 中的 setContentView,以便它在不附加到根目录的情况下扩展视图(最后一个参数设置为 false)。
    5. 在您的主要活动中,创建对话框,调用 Config.init(),并为 CordovaWebview 调用 loadUrl。

      公共类 CordovaDialog 扩展 Dialog 实现 CordovaInterface { CordovaInterface 父CordovaInterface; 上下文当前上下文;

      public CordovaDialog(Context context, CordovaInterface ci) {
          super(context);
          this.parentCordovaInterface = ci;
          this.currentContext = context;
      }
      
      @Override public void setContentView(int layoutResID) {
          final LayoutInflater inflater = LayoutInflater.from(this.currentContext);
          View v = inflater.inflate(layoutResID, null, false);
          super.setContentView(v);
      };
      
      @Override
      public Activity getActivity() {
          return this.parentCordovaInterface.getActivity();
      }
      
      @Override
      public ExecutorService getThreadPool() {
          return this.parentCordovaInterface.getThreadPool();
      }
      
      @Override
      public Object onMessage(String arg0, Object arg1) {
          return this.parentCordovaInterface.onMessage(arg0, arg1);
      }
      
      @Override
      public void setActivityResultCallback(CordovaPlugin plugin) {
          this.parentCordovaInterface.setActivityResultCallback(plugin);
      }
      
      @Override
      public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
          this.parentCordovaInterface.startActivityForResult(command, intent, requestCode);
      }
      }
      

    然后在您实现 CordovaInterface 的活动中:

    final CordovaDialog dialog = new CordovaDialog(this, this);
    dialog.setOwnerActivity(this);
    dialog.setContentView(R.layout.dialog_with_cordovawebview);
    
    CordovaWebView cwv = (CordovaWebView) dialog.findViewById(R.id.webViewDialog);
    Config.init();
    cwv.loadUrl("file:///android_asset/www/index.html");
    
    dialog.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多