相关问题是here
制作android应用,我认为我的代码很酷。
因为,无论何时需要对话框,我都会创建新的AlderDialog.Builder ,以防止出现此错误
“指定的孩子已经有一个父母。必须首先在该孩子的父母上调用removeView()。”
我的代码在这里
mDisposable.add(
RxView.clicks(fabTodoAdd).subscribe(aVoid -> {
initBuilder();
builder.show();
})
);
private void initBuilder() {
if (getActivity() != null) {
builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add Your Todo");
builderInput = new EditText(getActivity());
builderInput.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(builderInput);
builder.setPositiveButton("OK", (dialog, which) -> builderInput.getText());
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
}
}
有什么办法只能使一个时间生成器,并且不调用(init,show)而是仅显示吗?
答案 0 :(得分:1)
有一种非常简单的方法来制作具有自定义布局的对话框: 请查看以下代码:
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_news_description);//Your custom layout
TextView sometextview = dialog.findViewById(R.id.textView);// Textview in your custom layout
Button somebutton = dialog.findViewById(R.id.button_done);// Button in your layout
somebutton.setOnClickListener(new View.OnClickListener() {//on button click listener
@Override
public void onClick(View view) {
//DO your job....
//then...
dialog.dismiss();//dismiss the dialog
}
});
dialog.show();