【问题标题】:How to detect user interaction on a Dialog?如何检测对话框上的用户交互?
【发布时间】:2017-03-03 11:54:57
【问题描述】:

我可以使用此代码检测活动中的用户交互:

@Override
public void onUserInteraction() {
    super.onUserInteraction();
}

但是 Android Dialog 没有这样的方法。我找不到处理它的方法。

我该怎么做?

【问题讨论】:

  • 在显示对话框时是否尝试过在活动中获得 onUserInteraction 事件?如果是,您可以跟踪是否显示对话框并相应地处理案例。
  • 我无法在我的活动中获取事件。
  • 你有什么解决办法吗??
  • @Burak 对此有什么解决办法吗?

标签: android dialog user-interaction


【解决方案1】:

您可以使用 AppCompatActivity 样式化为带有样式的对话框,而不是常规的 Android 对话框

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
</style>

那么你就可以使用Activity类的功能了

您还应该使用以下 onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(android.support.v7.appcompat.R.layout.abc_alert_dialog_material);
}

要使其表现得像对话框一样,您需要手动找到需要将点击侦听器附加到每个按钮的所有视图。

以下是一些您可能会觉得有用的 ID:

@BindView(android.support.v7.appcompat.R.id.topPanel)
protected ViewGroup mTopPanel;
@BindView(android.support.v7.appcompat.R.id.contentPanel)
protected ViewGroup mContentPanel;
@BindView(android.support.v7.appcompat.R.id.customPanel)
protected ViewGroup mCustomPanel;
@BindView(android.support.v7.appcompat.R.id.custom)
protected ViewGroup mCustomViewContainer;
@BindView(android.support.v7.appcompat.R.id.textSpacerNoButtons)
protected View mTextSpacer;
@BindView(android.support.v7.appcompat.R.id.buttonPanel)
protected ButtonBarLayout mButtonPanel;

@BindView(android.R.id.message)
protected TextView mMessage;
@BindView(android.R.id.button1)
protected Button mPositiveButton;
@BindView(android.R.id.button2)
protected Button mNegativeButton;
@BindView(android.R.id.button3)
protected Button mNeutralButton;

(包括 Butterknife 注释,如果您不使用 Butterknife,只需使用 findViewById 和 @BindView(id) 中的每个注释)

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 2013-06-11
    • 2015-11-11
    • 2012-07-31
    • 2013-03-07
    • 1970-01-01
    • 2014-09-10
    • 2021-07-07
    • 1970-01-01
    相关资源
    最近更新 更多