我需要为这些项目显示一个AlertDialog带有 aListView和一个上下文菜单ListView。我更喜欢使用AlertDialog.Builder和调用setItems(),所以它为我Builder创建了一个带有风格化布局的ListView内部。AlertDialog对于样式化,它使用内部 Android 资源,因此我无法在我的代码中重新实现它。
问题是我无法捕获上下文菜单项单击事件,因为默认AlertDialog.onMenuItemSelected()实现不会将此类事件转发给父级:
public boolean onMenuItemSelected(int featureId, MenuItem item) {
return false;
}
我不能扩展AlertDialog.Builder类并强制它创建我自己的实例,AlertDialog因为onMenuItemSelected()我需要重写AlertDialog.Builder.create()它。但它使用了一个私有P变量,不能从派生类访问:
public AlertDialog create() {
final AlertDialog dialog = new AlertDialog(P.mContext, mTheme, false);
P.apply(dialog.mAlert);
dialog.setCancelable(P.mCancelable);
if (P.mCancelable) {
dialog.setCanceledOnTouchOutside(true);
}
dialog.setOnCancelListener(P.mOnCancelListener);
if (P.mOnKeyListener != null) {
dialog.setOnKeyListener(P.mOnKeyListener);
}
return dialog;
}
有没有办法强制AlertDialog.Builder构建一个自定义AlertDialog(onMenuItemSelected方法被覆盖)?