我试图制作一个可以在点击时被解雇的DialogFragment,经过一些搜索后我决定采用这种方式:
public class ErrorDialogFragment extends RoboDialogFragment {
private static final String MESSAGE_ARG = "message";
private TextView text;
public ErrorDialogFragment newInstance (String message){
ErrorDialogFragment f = new ErrorDialogFragment();
Bundle args = new Bundle();
args.putString(MESSAGE_ARG, message);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.error_dialog_fragment, container, false);
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ErrorDialogFragment.this.dismiss();
}
});
text = (TextView) v.findViewById(R.id.error_dialog_text_textView);
text.setText(getArguments().getString(MESSAGE_ARG));
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
}
警告对话框可以包含自定义消息,并在点按时被解除。
您认为这是实现这一目标的更好方法吗?
感谢。
答案 0 :(得分:14)
您可以使用 dialog.setCanceledOnTouchOutside(true); ,如果您在对话框外触摸,它将关闭对话框。或
试用本教程http://iserveandroid.blogspot.com/2010/11/how-to-dismiss-custom-dialog-based-on.html。 希望它有助于...... !!
答案 1 :(得分:2)
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(msg);
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
alertDialog.dismiss();
}
});
alertDialog.setIcon(R.drawable.error_icon);
alertDialog.show();
只要您想显示警报,只需使用此代码,并在对话框中解除其ok onclick事件。
答案 2 :(得分:1)
我从一个Activity调用DialogFragment。单击对话框中的按钮后,我使用界面调用活动内的方法。在那次活动中,我执行了这个:
// This is the code inside the activity that call the dialog
Fragment fragment = getSupportFragmentManager().findFragmentByTag("MyDialog");
if(fragment != null) {
DialogFragment dialog = (DialogFragment) fragment;
dialog.dismiss();
}