如何在Dialog上检测用户交互?

时间:2016-10-21 08:43:04

标签: android dialog user-interaction

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

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

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以将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)中的每个注释一起使用)