- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的应用程序中有一个 DialogFragment
的子类 (android.support.v4.app.DialogFragment
),它在很长一段时间内都运行良好。对话框本身是在 onCreateDialog()
回调中构建的。
但是,现在我想转换到新的对话框布局,并决定构建一个完全自定义的对话框。据我所知,这需要删除 onCreateDialog()
的覆盖并在 onCreateView()
回调中扩充 View
层次结构。我这样做了。
由此产生的行为非常奇怪——当需要显示对话框时,屏幕变暗,但不显示对话框的布局(“后退”按钮的行为也与对话框的功能一致):
我试图恢复到旧的实现(使用 onCreateDialog()
回调)——它仍然有效。
我的问题是:我做错了什么?
对话框的代码:
/**
* A dialog that can show title and message and has two buttons. User's actions performed
* in this dialog will be posted to {@link EventBus} as {@link PromptDialogDismissedEvent}.
*/
public class PromptDialog extends BaseDialog {
/* package */ static final String ARG_TITLE = "ARG_TITLE";
/* package */ static final String ARG_MESSAGE = "ARG_MESSAGE";
/* package */ static final String ARG_POSITIVE_BUTTON_CAPTION = "ARG_POSITIVE_BUTTON_CAPTION";
/* package */ static final String ARG_NEGATIVE_BUTTON_CAPTION = "ARG_NEGATIVE_BUTTON_CAPTION";
@Inject EventBus mEventBus;
private TextView mTxtTitle;
private TextView mTxtMessage;
private Button mBtnPositive;
private Button mBtnNegative;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
getControllerComponent().inject(this);
}
// THIS CODE MAKES THE SCREEN DIM, BUT THE ACTUAL LAYOUT IS NOT SHOWN
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_info_prompt, container, false);
initSubViews(rootView);
populateSubViews();
return rootView;
}
private void initSubViews(View rootView) {
mTxtTitle = (TextView) rootView.findViewById(R.id.txt_dialog_title);
mTxtMessage = (TextView) rootView.findViewById(R.id.txt_dialog_message);
mBtnPositive = (Button) rootView.findViewById(R.id.btn_dialog_positive);
mBtnNegative = (Button) rootView.findViewById(R.id.btn_dialog_negative);
mBtnPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE));
}
});
mBtnNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE));
}
});
}
private void populateSubViews() {
String title = getArguments().getString(ARG_TITLE);
String message = getArguments().getString(ARG_MESSAGE);
String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION);
String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION);
mTxtTitle.setText(TextUtils.isEmpty(title) ? "" : title);
mTxtMessage.setText(TextUtils.isEmpty(message) ? "" : message);
mBtnPositive.setText(positiveButtonCaption);
mBtnNegative.setText(negativeButtonCaption);
}
// THE BELOW CODE WORKS FINE (the dialog is shown and responsive)
// @Override
// public @NonNull Dialog onCreateDialog(Bundle savedInstanceState) {
// String title = getArguments().getString(ARG_TITLE);
// String message = getArguments().getString(ARG_MESSAGE);
// String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION);
// String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION);
//
// Dialog dialog = new AlertDialog.Builder(getActivity())
// .setTitle(TextUtils.isEmpty(title) ? "" : title)
// .setMessage(TextUtils.isEmpty(message) ? "" : message)
// .setPositiveButton(TextUtils.isEmpty(positiveButtonCaption) ? "" : positiveButtonCaption,
// new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE));
// }
// })
// .setNegativeButton(TextUtils.isEmpty(negativeButtonCaption) ? "" : negativeButtonCaption,
// new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE));
// }
// })
// .create();
//
// return dialog;
// }
}
自定义对话框的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
style="@style/AppTheme.TextView.DialogTitle"
tools:text="Dialog title"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="@color/gray"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/fragment_default_padding">
<TextView
android:id="@+id/txt_dialog_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
style="@style/AppTheme.TextView.DialogMessage"
tools:text="Dialog message very very very very very very very very very very very very long"/>
<Button
android:id="@+id/btn_dialog_positive"
android:layout_width="wrap_content"
android:layout_height="@dimen/button_height"
android:layout_marginTop="15dp"
android:layout_below="@id/txt_dialog_message"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
style="@style/AppTheme.GreenButton"
tools:text="Positive"/>
<Button
android:id="@+id/btn_dialog_negative"
android:layout_width="wrap_content"
android:layout_height="@dimen/button_height"
android:layout_marginTop="15dp"
android:layout_below="@id/txt_dialog_message"
android:layout_toStartOf="@+id/btn_dialog_positive"
android:layout_toLeftOf="@id/btn_dialog_positive"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
style="@style/AppTheme.GrayButton"
tools:text="Negative"/>
</RelativeLayout>
</LinearLayout>
最佳答案
我最终采用了这种解决方法,但我仍然想了解 WTF:
// This is a workaround for the strange behavior of onCreateView (which doesn't show dialog's layout)
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = LayoutInflater.from(getContext());
View dialogView = inflater.inflate(R.layout.dialog_info_prompt, null);
dialogBuilder.setView(dialogView);
initSubViews(dialogView);
populateSubViews();
setCancelable(false);
return dialogBuilder.create();
}
private void initSubViews(View rootView) {
mTxtTitle = (TextView) rootView.findViewById(R.id.txt_dialog_title);
mTxtMessage = (TextView) rootView.findViewById(R.id.txt_dialog_message);
mBtnPositive = (Button) rootView.findViewById(R.id.btn_dialog_positive);
mBtnNegative = (Button) rootView.findViewById(R.id.btn_dialog_negative);
mBtnPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE));
}
});
mBtnNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE));
}
});
}
private void populateSubViews() {
String title = getArguments().getString(ARG_TITLE);
String message = getArguments().getString(ARG_MESSAGE);
String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION);
String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION);
mTxtTitle.setText(TextUtils.isEmpty(title) ? "" : title);
mTxtMessage.setText(TextUtils.isEmpty(message) ? "" : message);
mBtnPositive.setText(positiveButtonCaption);
mBtnNegative.setText(negativeButtonCaption);
}
关于Android:支持未显示自定义布局的 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41730660/
我的 processmaker 安装遇到了一些问题。我正在尝试使用本指南 [url]http://wiki.processmaker.com/index.php/ProcessMaker_Ubuntu
我正在使用 ShareKit。发送 SMS 消息使用 MFMessageComposeViewController,用户看到标题“文本”。我想将该标题更改为更能反射(reflect)实际可用内容的内容
我需要在我的一个针对 Gingerbread 的 Android 应用程序中使用操作栏和 fragment 的组合。所以我使用了 v7 支持库中的操作栏和 v4 支持库中的 fragment ,并使用
我明白为什么浏览器 vendor 不想帮助我阻止他们的 UI 线程。但是,我不明白为什么会有: Web Workers 中没有 sleep (2) 没有同步 WebSockets API 有一个syn
最近我的组织正在考虑使用 Docker。我们组使用的是cloudera CDH 5.1.2。 1) cloudera 是否与 Docker 容器兼容?2) docker 和cloudera 组合是否存
我正在尝试通过编译在 Mac 上安装 rsync 3.2.3。但是,我想安装所有功能。为此,它需要一些库,此处 ( https://download.samba.org/pub/rsync/INSTA
我一直在使用 PyDev 成功运行 nose 测试,并想试试 nose2。 所以我安装了它 pip install nose2 复制/粘贴来自 http://nose2.info/ 的示例代码进入名为
我想知道 LLVM 中是否有任何函数/方法可以在 LLVM IR 中添加 Open-MP 构造。 llvm-3.0 是否仍然支持 OpenMP 指令? 最佳答案 OpenMP 是一种高级语言扩展。因此
我对 CUDA 编程非常陌生。我正在浏览 SDK 附带的示例。我能够编译代码,但是当我运行它时,出现以下错误: "clock.cu(177) : CUDA Runtime API error 38:
RStudio 是用于 R 开发的出色 IDE。我想知道是否有任何方法可以很好地支持 HiDPI 分辨率? 我目前有 13 英寸显示器和 3200x1800 分辨率,甚至很难阅读 RStudio 选项
我正在寻找一种有助于为 Django 项目提供 RDF 支持的工具。 到目前为止,我发现了两个: django-rdf - 最后一次修改是在 4 年前,所以它看起来像是一个死项目。 djubby -
我刚刚尝试了一些 JS 核心原则,发现引擎评估链接的关系运算符而不会引发错误。相反,他们以我自己无法理解的方式进行评估。 console.log(1 4 > 3 > 2 > 1); //false,
我知道 etexteditor 和 vim/emacs。 是否有任何其他 Windows 编辑器支持类似 textmate 的片段(例如,您编写触发词,按 Tab,它更改为某些内容,再次按 Tab,它
我正在尝试找出验证给定集群的网络策略配置的最佳方法。 According to the documentation Network policies are implemented by the ne
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
Z3 会支持 AUFBV 吗? 对于以下脚本: (set-logic AUFBV) (declare-fun x () (_ BitVec 16)) (declare-const t (Array (
使用分部类编写 NUnit 测试的优缺点是什么? 我要开始了: 亲:可以测试私有(private)方法 缺点:TDD 不再可能了 还有什么? 最佳答案 缺点:要么您必须测试与您发布的版本不同的构建,要
它很容易(对于 90% 的 aop 特性)在没有任何语言本身支持的情况下做到这一点,就像大多数动态语言如 python 和 ruby 一样。然而,Dojo在 1.3.2 上直接支持它.最新版本发生
我在我的 android 应用程序中使用亚洲字符,我已经了解到某些字符无法显示,因为系统字体不支持它们。我查询了一个包含亚洲字符的数据库,并且经常检索到无法显示的标志。这些情况对我的应用程序来说通常不
你好,我想实现一个控件,我想在用户键入@字符时启用该控件,直到未填充运行文本中的空格为止,它应该显示用户列表,@符号后键入的文本应该显示基于键盘字符的建议,就像我们在上面看到的那样Twitter 或
我是一名优秀的程序员,十分优秀!