防止在android中显示按钮按下的多个意图

时间:2016-12-08 11:11:30

标签: android android-intent xamarin.android

我已经实现了UI有一个按钮的代码。按下按钮,我将从意图中提示电子邮件选择器。

问题是当我多次按下按钮时,意图弹出窗口逐个显示。我可以通过使用标志来阻止它吗?这是一个Xamarin Android项目。

代码:

Intent intent = new Intent();
intent.SetAction(Intent.ActionSend);
intent.SetType(FileType);
intent.PutExtra(Intent.ExtraSubject, emailMessage.Subject);
if (!emailMessage.IsLinkInclude && !emailMessage.IsAlternateFileInclude){
    intent.PutExtra(Intent.ExtraStream, AssetPathHelper.GetAssetUri(item, context));
}

intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.ResetTaskIfNeeded);
intent.PutExtra(Intent.ExtraText, Html.FromHtml(emailMessage.Message));
if (intent.ResolveActivity(context.PackageManager) != null){
    context.StartActivity(Intent.CreateChooser(intent, AndroidStringLoader.GetStringValue(Resource.String.SendEmailText)));
}

3 个答案:

答案 0 :(得分:1)

禁用onClick()中的按钮,一旦完成预期的操作,启用按钮。

public void onClick(View view) {
    btn.setEnabled(false);
    //some operation may be async
}

public void onOperationFinished() {
    btn.setEnabled(true);
}

答案 1 :(得分:0)

我遇到了同样的问题,这是我的解决方案,通过在两次连续点击之间放置一个计时器来避免它。

private static final long INTERVAL = 1000;
private long lastClickTime = 0;

// onClick of your button code below
@Override
public final void onClick(View v) {
    long currentTime = SystemClock.elapsedRealtime();
    if (currentTime - lastClickTime > INTERVAL) {
        lastClickTime = currentTime;
        //perform the task here of click
    }
}

另一种方式是setEnabled(false),但是对于非常快速的点击按钮,效率不高。

答案 2 :(得分:0)

似乎在这个场景中没有传递意图的标志,所以我通过以下方式实现了它,我们可以从中获得失败和成功状态

https://developer.android.com/guide/components/activities.html#StartingAnActivityForResult

browser=ENV['BROWSER'] || 'ff'
case browser
  when 'ff', 'firefox'
    Capybara.register_driver :selenium do |app|
      Selenium::WebDriver::Firefox::Binary.path=("/usr/bin/firefox") if REGISTRY[:local_path_for_selenium]
      profile = Selenium::WebDriver::Firefox::Profile.new
      profile.assume_untrusted_certificate_issuer = false
      profile.secure_ssl = false
      profile['browser.manage.timeouts.implicit_wait'] = 100
      profile['browser.manage.timeouts.script_timeout'] = 100
      profile['browser.manage.timeouts.read_timeout'] = 500
      profile['browser.manage.timeouts.page_load'] = 120
      profile["browser.download.folderList"] = 2
      profile['browser.download.dir'] = "#{Rails.root}/downloads"
      profile['browser.helperApps.neverAsk.saveToDisk'] = "application/xlsx"
      profile['browser.helperApps.neverAsk.openFile'] = "application/xlsx"
      http_client = Selenium::WebDriver::Remote::Http::Default.new
      http_client.timeout = 410
      Capybara::Selenium::Driver.new(app, :profile => profile, :http_client => http_client)
    end
    Capybara.default_driver = :selenium