2014-03-24 21 views
2

我可以在android中的CustomDialog中实现Cordova WebView吗? 我想单击一个按钮,这会显示一个带有webview的对话框。我以这种方式尝试,但没有奏效。在对话框中的Cordova 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(); 
    } 
}); 

回答

0

UPDATE2:

事实上,扩展对话框并不事件需要实现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都找不到与该对话框关联的Activity,logcat显示一条警告,说我的Activity没有实现CordovaInterface,但它确实如此。

更新: 好的,我想通了。所以我就是这样。这很长,但它的工作原理。

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

    public class CordovaDialog extends Dialog implements CordovaInterface CordovaInterface parentCordovaInterface; 上下文currentContext;

    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();