简体   繁体   English

如何在 Rubymotion webview 中添加确认 AlertDialog?

[英]How do I add a confirm AlertDialog in a Rubymotion webview?

Using the android webview template there is an example for handling js alerts.使用 android webview 模板有一个处理 js 警报的示例。

class MyWebChromeClient < Android::Webkit::WebChromeClient
  def onJsAlert(view, url, message, result)
    puts message
    result.confirm
    true
  end
end

How would I implement this for confirm with the builtin Dialog.Builder ?我将如何使用内置的Dialog.Builder进行confirm

The trick was to pass the view.context to the Builder and add listeners that handle the onClick :诀窍是将 view.context 传递给Builder并添加处理onClick的侦听器:

class MyWebChromeClient < Android::Webkit::WebChromeClient
  def onJsConfirm(view, url, message, result)
    builder = Android::App::AlertDialog::Builder.new(view.context)
    builder.message = message
    builder.setPositiveButton(Android::R::String::Ok, ConfirmListener.new(result))
    builder.setNegativeButton(Android::R::String::Cancel, CancelListener.new(result))
    builder.show
    true
  end
end

class ConfirmListener
  attr_reader :result
  def initialize(result)
    @result = result
  end

  def onClick(dialog, id)
    result.confirm
  end
end

class CancelListener
  attr_reader :result
  def initialize(result)
    @result = result
  end

  def onClick(dialog, id)
    result.cancel
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM