我已经写了一个DialogFragment。现在我意识到我希望它像AlertDialog一样有一个正面和负面的按钮。如何在保持我编写的代码的同时实现这样的目标?
public class DoublePlayerChooser extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL,0);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("title")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do something...
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
)
.create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.doubleplayerchooser, container, false);
getDialog().setTitle("Enter Players");
firstPlayerPicker = (ImageButton) v.findViewById(R.id.imageButton1);
firstPlayerPicker.setOnClickListener(new OnClickListener() {
public void onClick(final View v){
callContactPicker(1);
}
});
secondPlayerPicker = (ImageButton) v.findViewById(R.id.ImageButton01);
secondPlayerPicker.setOnClickListener(new OnClickListener() {
public void onClick(final View v){
callContactPicker(2);
}
});
loadFromFile = (ImageButton) v.findViewById(R.id.imageButton2);
loadFromFile.setOnClickListener(new OnClickListener() {
public void onClick(final View v){
}
});
firstTextfield = (EditText) v.findViewById(R.id.editText1);
secondTextfield = (EditText) v.findViewById(R.id.EditText01);
firstImage = (ImageView) v.findViewById(R.id.imageView1);
secondImage = (ImageView) v.findViewById(R.id.ImageView01);
return v;
}
答案 0 :(得分:67)
这就是我弄清楚的方法。我删除了onCreateView并更改了onCreateDialog。 这个link实际上有答案所以所有的功劳都应该去那里。我刚发布它,以防万一有人先讨论这个问题。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder b= new AlertDialog.Builder(getActivity())
.setTitle("Enter Players")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do something...
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
);
LayoutInflater i = getActivity().getLayoutInflater();
View v = i.inflate(R.layout.doubleplayerchooser,null);
firstPlayerPicker = (ImageButton) v.findViewById(R.id.imageButton1);
firstPlayerPicker.setOnClickListener(new OnClickListener() {
public void onClick(final View v){
callContactPicker(1);
}
});
secondPlayerPicker = (ImageButton) v.findViewById(R.id.ImageButton01);
secondPlayerPicker.setOnClickListener(new OnClickListener() {
public void onClick(final View v){
callContactPicker(2);
}
});
loadFromFile = (ImageButton) v.findViewById(R.id.imageButton2);
loadFromFile.setOnClickListener(new OnClickListener() {
public void onClick(final View v){
}
});
firstTextfield = (EditText) v.findViewById(R.id.editText1);
secondTextfield = (EditText) v.findViewById(R.id.EditText01);
firstImage = (ImageView) v.findViewById(R.id.imageView1);
secondImage = (ImageView) v.findViewById(R.id.ImageView01);
b.setView(v);
return b.create();
}
答案 1 :(得分:6)
您必须覆盖DialogFragments onCreateDialog(...)方法:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("title")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do something...
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
)
.create();
}
从这里采取:Android: disable DialogFragment OK/Cancel buttons
根据您收到的错误消息(“请求功能必须被调用...”)我建议:
不要在您的Activity中的requestFeature()之前调用setContentView(),也不要在调用它的任何地方调用它。
此外:
不要在onCreate()中调用 setStyle(...)。
在创建片段的地方调用它。
YourDialogFragment f = new YourDialogFragment(Context);
f.setStyle(...);
// and so on ...
答案 2 :(得分:1)
要添加操作按钮,请调用setPositiveButton()
和setNegativeButton()
方法:
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
有关DialogFragment here的更多信息。
答案 3 :(得分:1)
这有点旧,但最近我在扩展onCreateView
时一直在覆盖AppCompatDialogFragment
。只需将您自己的按钮放在onCreateView
中返回的相同布局中 - 使用@style/Widget.AppCompat.Button.Borderless
等样式。
单击操作按钮时,您可以获得控制Dialog自解除的额外好处,特别是因为这些自定义视图有时需要输入,并且您希望在单击按钮时阻止自动关闭Dialog。 / p>
在onCreateDialog
中使用自定义视图一直感觉很脏,因为你在没有容器的情况下给它充气。 Google尝试使用新的v7 AlertDialog.Builder
方法setView(int layoutResId)
使API更好一些,但您无法再调用findViewById
。
您应该在styles.xml中添加这样的主题:
<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">@color/material_light_blue_500</item>
<item name="colorPrimaryDark">@color/material_light_blue_800</item>
<item name="colorAccent">@color/material_light_blue_a400</item>
<item name="colorButtonNormal">@color/material_light_blue_500</item>
<item name="colorControlNormal">@color/material_light_blue_600</item>
<item name="colorControlActivated">@color/material_light_blue_a100</item>
<item name="colorControlHighlight">@color/material_light_blue_a100</item>
</style>
您必须覆盖DialogFragment中的onCreateDialog
以返回new AppCompatDialog(getActivity(), R.style.AlertDialog)
。