Android 取消对话框片段onClick
我试图制作一个DialogFragment,当点击它时可以将其删除,经过一些搜索后,我决定使用这个实现:Android 取消对话框片段onClick,android,dialog,android-fragments,Android,Dialog,Android Fragments,我试图制作一个DialogFragment,当点击它时可以将其删除,经过一些搜索后,我决定使用这个实现: public class ErrorDialogFragment extends RoboDialogFragment { private static final String MESSAGE_ARG = "message"; private TextView text; public ErrorDialogFragment newInstance (String message){
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);
}
警报对话框可以有一条自定义消息,点击时将被取消
你认为实现这一目标的更好方法是什么
谢谢
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事件。您可以使用对话框。SetCanceledOnTouchOut(true)如果在对话框外部触摸,将关闭对话框。或 试试这个教程。
希望对你有帮助 我正在调用活动中的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();
}
谢谢@user1208720,但我的类是从DialogFragment(故意不是AlertDialog)扩展而来的,当单击按钮时,它不会关闭,而当单击视图上的任何位置时,它也不会关闭。问题是如何从android框架中使用这个DialogFragment类,以及如何正确地分派如下事件:)感谢@Raghav我现在使用setCanceledOnTouchOutside,但是对于inside tap,我将继续我的方法,链接既不指向活动,也不指向对话框片段:)您能告诉我在哪里调用setCanceledOnTouchOutside方法吗?因为我需要dialog对象,所以只要把它写在执行dialog所有其他操作的地方就行了。