此DialogFragment实现会导致
IllegalStateException(“你不能设置Dialog的OnCancelListener或 OnDismissListener“)
。为什么?溶液
public class OkCThreadDialog1 extends DialogFragment{
DialogInterface.OnCancelListener onCancelListener;
public OkCThreadDialog1(){
}
public static OkCThreadDialog1 newInstance(String title, String message) {
OkCThreadDialog1 frag = new OkCThreadDialog1();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
frag.setArguments(args);
return frag;
}
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder .setTitle(getArguments().getString("title"))
.setMessage(getArguments().getString("message"))
.setOnCancelListener(onCancelListener)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
getDialog().cancel();
}});
return builder.create();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
onCancelListener = (DialogInterface.OnCancelListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement OkCancelDialogListener");
}
}
}
我的活动实施DialogInterface.OnCancelListener
,如下所示:
public class MainActivity extends Activity implements OkCancelDialogListener{
static final String TAG ="MainActivity";
@Override
public void onCancel(DialogInterface dialog) {
}
}
从builder.create();
抛出异常。怎么了?
答案 0 :(得分:87)
来自Android文档:
public Dialog onCreateDialog(Bundle savedInstanceState)
覆盖以构建您自己的自定义Dialog容器。这是 通常用于显示AlertDialog而不是通用Dialog; 这样做时,onCreateView(LayoutInflater,ViewGroup,Bundle)就可以了 因为AlertDialog会自己处理,所以不需要实现 内容。
此方法将在onCreate(Bundle)之后和之前调用 onCreateView(LayoutInflater,ViewGroup,Bundle)。默认 实现只是实例化并返回一个Dialog类。
注意:DialogFragment拥有Dialog.setOnCancelListener和Dialog.setOnDismissListener回调。你不能自己设置它们。
要了解这些事件,请覆盖onCancel(DialogInterface)和 onDismiss(DialogInterface)。强>
基本上,你必须覆盖onDismiss或OnCancel而不是'.setOnCancelListener(onCancelListener)'。
答案 1 :(得分:6)
你可以试试这个:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something positive
}
});
return builder.create();
}
@Override
public void onCancel(DialogInterface dialog) {
// do something
}
希望它可以帮助...
答案 2 :(得分:0)
为对话框设置OnCancelListener而不是其构建器。
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder .setTitle(getArguments().getString("title"))
.setMessage(getArguments().getString("message"))
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
getDialog().cancel();
}});
Dialog dialog = builder.create();
dialog.setOnCancelListener(onCancelListener);
return dialog;
}
答案 3 :(得分:-3)
试试这段代码我希望这会有用..
AlertDialog.Builder builder = new AlertDialog.Builder(
class_name.this);
builder.setTitle("Title");
AlertDialog dialog = builder.create();
dialog.setButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
});
dialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();